【springboot+ajax】阿里云短信服务

发送短信验证码

开通阿里云短信服务

查看AccessKey
`AccessKey
短信服务管理控制台
短信签名
短信模板

Springboot后台接口

  1. pom.xml增加加短信服务相关依赖
		<dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.0.8</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>
  1. 短信发送模块,MsgSend 类
package com.education.edu_server.util;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * @Description:
 * @Author: Lorogy
 * @Date: 2020/4/8 15:15
 */
public class MsgSend {
    
    
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    static final String accessKeyId = "XXXX";  // TODO 修改成自己的
    static final String accessKeySecret = "XXXX";

    public SendSmsResponse sendSms(String telephone, String code) throws ClientException {
    
    
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(telephone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName("XXXX");    // TODO 修改成自己的
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode("XXXXX");    // TODO 修改成自己的
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
//        request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");
        request.setTemplateParam("{\"code\":\"" + code + "\"}");
        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");
        //hint 此处可能会抛出异常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
    
    
            System.out.println("短信发送成功!");
        }else {
    
    
            System.out.println("短信发送失败!");
        }
        return sendSmsResponse;
    }


/*
    不删 留着
    以后可能有用

    public static QuerySendDetailsResponse querySendDetails(String bizId) throws ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        QuerySendDetailsRequest request = new QuerySendDetailsRequest();
        //必填-号码
        request.setPhoneNumber("15000000000");
        //可选-流水号
        request.setBizId(bizId);
        //必填-发送日期 支持30天内记录查询,格式yyyyMMdd
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        request.setSendDate(ft.format(new Date()));
        //必填-页大小
        request.setPageSize(10L);
        //必填-当前页码从1开始计数
        request.setCurrentPage(1L);
        //hint 此处可能会抛出异常,注意catch
        QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
        return querySendDetailsResponse;
    }
*/
}

  1. 处理6位数字短信验证码生成、发送及校验,controller
package com.education.edu_server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.education.edu_server.util.MsgSend;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Lorogy
 * @Date: 2020/4/8 14:24
 */
@RestController
@RequestMapping("/msg")
public class SendMsgController {
    
    
    @Autowired
    @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    /**
     * 发送6位短信验证码到用户手机号
     * @param map
     * @return
     * @throws ClientException
     */
    @RequestMapping("/sendMsg")
    public String sendMsg(@RequestBody Map map) throws ClientException {
    
    
        // 获取用户手机号
        System.out.println(map.get("phoneNum"));
        String phoneNum = (String) map.get("phoneNum");
        JSONObject res = new JSONObject();

        // 随机生成六位随机数
        StringBuffer stringBuffer = new StringBuffer();
        for (int x = 0; x <= 5; x++) {
    
    
            int random = (int) (Math.random() * (10 - 1));
            stringBuffer.append(random);
        }
        String code = stringBuffer.toString();

        System.out.println("发送的验证码为:"+code);
        // 发短信
        MsgSend msgSend=new MsgSend();
        SendSmsResponse response =msgSend.sendSms(phoneNum,code); // TODO 填写你需要测试的手机号码
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());// 成功,OK
        System.out.println("Message=" + response.getMessage());// 成功,OK
        System.out.println("RequestId=" + response.getRequestId());//请求ID
        System.out.println("BizId=" + response.getBizId());//发送回执ID

        // 记录到数据库
        String sql = "insert into msg (phone,code,requestid,bizid,msg,datetime) values (?,?,?,?,?,?)";
        int count=jdbcTemplate.update(sql, phoneNum,code, response.getRequestId(), response.getBizId(), response.getCode(),new Date().getTime());
        System.out.println(count);

        // 返回发送结果
        res.put("code",response.getCode());
        res.put("requestId", response.getRequestId());
        return JSON.toJSONString(res);
    }

    /**
     * 验证用户输入的验证码是否正确
     * @param map
     * @return
     */
    @RequestMapping("/checkCode")
    public String checkCode(@RequestBody Map map){
    
    
        // 获取用户手机号、用户输入的验证码、以及请求id
        String phoneNum = (String) map.get("phoneNum");
        String code = (String)map.get("code");
        String requestId = (String) map.get("requestId");
        // 数据库查询该发送记录,从而判断验证码是否正确
        String sql = "select datetime from msg where code=? and phone=? and requestid=? order by datetime desc";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,code,phoneNum,requestId);
        JSONObject res = new JSONObject();
        
        if(list.size()>0&&list!=null){
    
    
            Object str=list.get(0).get("datetime");
            Long time= Long.parseLong(str.toString());
            Date now=new Date();
            // 判断时间是否超过半小时
            if(now.getTime()-time>1000*60*30){
    
    
                res.put("code", false);
                res.put("msg", "超时");
            }else{
    
    
                res.put("code", true);
                res.put("msg", "验证成功");
            }
        }else {
    
    
            res.put("code", false);
            res.put("msg", "验证码错误");
        }

        return JSON.toJSONString(res);
    }

}

前端验证demo

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>api-test</title>
	<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>

<body>
	<div>
		<div>
			<label for="phonenum">手机号:<input id="phonenum" name="phonenum" type="text" /></label>
			<button id="sendMsg">发送</button>
		</div>
		<div>
			<label for="code">验证码:<input id="code" name="code" type="text" /></label>
		</div>
		<div><button id="checkMsg">提交</button></div>
	</div>
	<script>
		var requestId = "",codeNum="";

		$('#sendMsg').click(() => {
    
    
			let phonenum = $("#phonenum").val()
			console.log(phonenum)

			$.ajax({
    
    
				type: "POST", 
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				url: "http://127.0.0.1:8082/msg/sendMsg", 
				data: JSON.stringify({
    
     "phoneNum": phonenum }),

				error: function (request) {
    
      //失败的话
					console.log(request)
				},
				success: function (data) {
    
      //成功
					$('#sendMsg').hide()
					requestId=data.requestId
					codeNum=data.code
				}
			});
		});

		$('#checkMsg').click(() => {
    
    
			    let phonenum = $("#phonenum").val()
			    let code = $("#code").val()

				$.ajax({
    
    
					type: "POST",   
					dataType: "json",
					contentType: 'application/json;charset=UTF-8',
					url: "http://127.0.0.1:8082/msg/checkCode", 
					data: JSON.stringify({
    
     "phoneNum": phonenum ,"code":code,"requestId": requestId}),

					error: function (request) {
    
      //失败的话
						console.log(request)
					},
					success: function (data) {
    
      //成功
						if(data.code){
    
    
							$('#checkMsg').hide()
							alert("成功")
						}else{
    
    
							alert(data.msg)
						}

					}
				});
			});

	</script>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/lorogy/article/details/106252429