SSM项目之短信验证码登录

本文分享了如何使用Java SSM框架实现短信验证码登录功能,包括接入zz短信平台、添加POM依赖、引入MessageUtil工具类配置、设计Ajax模块、构建HTML页面以及编写Controller层。还提及了RandomValueUtil工具类用于生成随机验证码。
摘要由CSDN通过智能技术生成

第一步登入网站 zz短信平台

http://sms_developer.zhenzikj.com/zhenzisms_user/login.html

导入pom依赖

<dependency>
      <groupId>com.zhenzikj</groupId>
      <artifactId>zhenzisms</artifactId>
      <version>2.0.2</version>
    </dependency>

第二步导入工具类MessageUtil

package kj15.util;

import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class MessageUtil {
    private static String apiUrl;
    private static String appId;
    private static String appSecret;

        static {
            InputStream resourceAsStream = MessageUtil.class.getClassLoader().getResourceAsStream("messageUtil.properties");
            Properties properties = new Properties();
            try {
                properties.load(resourceAsStream);
                apiUrl = properties.getProperty("apiUrl");
                appId = properties.getProperty("appId");
                appSecret = properties.getProperty("appSecret");

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    public static boolean sendMessage(String number,String templateId,String randomCode,String timeDesc){
        if(apiUrl==null||appId==null||appSecret==null){
            return false;
        }
//        String apiUrl="https://sms_developer.zhenzikj.com";
//        String appId="111566";//在控制台的 我的应用
//        String appSecret="6831ecde-9823-40de-8354-bf33c7afd819";//在控制台的 我的应用
        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number",number);//你要发送的手机号
        params.put("templateId",templateId);//短信模板里的
        String[] templateParams = new String[2];//模板
        templateParams[0] = randomCode;
        templateParams[1] = timeDesc;
        params.put("templateParams", templateParams);

        String result = null;
        try {
            result = client.send(params);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        System.out.println(result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if("0".equals(jsonObject.get("code").toString())){
            return true;
        }else {
            return false;
        }
    }

}

配置messageUtil.properties

apiUrl=https://sms_developer.zhenzikj.com
appId=111233
appSecret=6831ecde-9823-40de-8354-17afd831119

第三步ajax 模块

 $("#send").click(function () {
        // $('#captcha').empty();
        jigsaw.init(document.getElementById('captcha'), function () {
            alert("验证成功");
            $('#captcha').empty();
            sendMsg($("#send"));

        });

    });

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值