美文网首页
SpringBoot关于验证码通知短信的开发Demo

SpringBoot关于验证码通知短信的开发Demo

作者: 问瑾遗 | 来源:发表于2019-02-28 01:11 被阅读0次

本文使用秒嘀科技的API,不才起初也找了几个,发现阿里云和腾讯云的需要企业认证之类的,网建的最低充值也要50大洋。无奈借用了朋友秒嘀科技的账号,听说进去还送10块钱的。

1.API接入流程:

API接入流程

2.代码开发流程

2.1 环境参数设置

application.yaml关于sms部分设置如下:

sms:
  url: https://api.miaodiyun.com/20150822/industrySMS/sendSMS
  accountSid: xxx
  templateid: xxx
  authToken: xxx

这里的几个参数值都是在秒嘀科技获取的。

2.2 pojo部分

秒嘀科技的API关于json格式返回示例(更多细节请参看官网API指南):

{
"respCode":"00000",
"respDesc":"成功",
"failCount":"1",
"failList":
[
    {
        "phone":"13896543210",
        "respCode":"00111",
    }
],
"smsId":"913945fec0204b1e94baa75a5c013f59"
}

由上述格式可新建类Result和failDetail,源码如下:

package com.xxx.sms.pojo;

/**
 * @author 
 * @create 
 */
public class failDetail {
    private String phone;
    private String respCode;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRespCode() {
        return respCode;
    }

    public void setRespCode(String respCode) {
        this.respCode = respCode;
    }
}
package com.xxx.sms.pojo;

import java.util.List;

/**
 * @author 
 * @create 
 */
public class Result {
    private String respCode;
    private String respDesc;
    private String failCount;
    private List<failDetail> failList;
    private String smsId;

    public String getRespCode() {
        return respCode;
    }

    public void setRespCode(String respCode) {
        this.respCode = respCode;
    }

    public String getRespDesc() {
        return respDesc;
    }

    public void setRespDesc(String respDesc) {
        this.respDesc = respDesc;
    }

    public String getFailCount() {
        return failCount;
    }

    public void setFailCount(String failCount) {
        this.failCount = failCount;
    }



    public List<failDetail> getFailList() {
        return failList;
    }

    public void setFailList(List<failDetail> failList) {
        this.failList = failList;
    }

    public String getSmsId() {
        return smsId;
    }

    public void setSmsId(String smsId) {
        this.smsId = smsId;
    }
}

2.3 HttpUtil部分

请求说明

由上图要求可编写如下类:

package com.xxx.sms.util;

import com.xxx.sms.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @author 
 * @create 
 */

@Component
public class HttpUtil {
    //获取application.yaml中配置的sms.url值
    @Value("${sms.url}")
    private String url;
    //发送post请求
    public Result sendPostRequest(MultiValueMap<String, String> requestBody){
        RestTemplate client=new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头和请求体合并
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
        //发送请求
        ResponseEntity<Result> response = client.exchange(url,
                HttpMethod.POST, requestEntity, Result.class);

        return response.getBody();
    }
}

2.4 smsMainTest部分

请求参数说明

可由上图构造请求体部分。
以下代码是整个项目中的一个模块,有关RabbitMQ的部分可忽略。

package com.xxx.sms.listener;

import com.xxx.sms.pojo.Result;
import com.xxx.sms.util.HttpUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@Component
@RabbitListener(queues = "sms")
public class SmsListener {

    @Autowired
    private HttpUtil client;
    //可由env获得application.yaml中配置
    @Autowired
    private Environment env;


    @RabbitHandler
    public void executeSms(Map<String,String> map){


        System.out.println("手机号:"+map.get("mobile"));
        System.out.println("验证码:"+map.get("checkcode"));

        // 时间戳
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(new Date());

        // 签名
        String sig = DigestUtils.md5Hex(env.getProperty("sms.accountSid")+
                env.getProperty("sms.authToken") + timestamp);
        //构造请求体部分
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("accountSid",env.getProperty("sms.accountSid"));
        body.add("templateid",env.getProperty("sms.templateid"));
        body.add("param",map.get("checkcode"));
        body.add("to",map.get("mobile"));
        body.add("timestamp",timestamp);
        body.add("sig",sig);
        body.add("respDataType","JSON");

        //发送
        Result result = client.sendPostRequest(body);

        System.out.println(result.getRespCode());
        System.out.println(result.getRespDesc());



    }
}

测试,成功运行。

相关文章

网友评论

      本文标题:SpringBoot关于验证码通知短信的开发Demo

      本文链接:https://www.haomeiwen.com/subject/odlcuqtx.html