Spring Boot:搭建一个简单的用户系统 - 发送短信验证码
一.准备工作
要发送短信验证码,需要提前准备要接入的短信网关;市面上短信网关太多了,接谁家那就让老板拍板吧,本文使用阿里云的短信网关进行说明。
- 申请短信网关
首先你得有个阿里云账号,进入短信服务 ; - 签名管理
- 模板管理
此流程有阿里的文档,就不详细说明了;
二.引入阿里云sdk
- pom.xml添加库引入
<!--阿里云的短信服务SDK-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
- application.properties添加配置信息
# 阿里云短信相关
accessKeyId=你自己的
accessKeySecret=你自己的
# 签名
signName=XXX
# 短信模板id
templateCode=XXX
product: Dysmsapi
domain: dysmsapi.aliyuncs.com
三.实体类定义
1.ResponseInfo
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseInfo {
private int code;
private String msg;
private Object data;
public ResponseInfo(int code,String msg){
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
2.ResponseConfig
public class ResponseConfig {
public static final int unknowCode = -1;
public static final String unknowMsg = "未知错误!";
public static final int defaultCode = 100;
public static final String defaultMsg = "操作成功!";
public static final String loginSucMsg = "登录成功!";
public static final int paramErrorCode = 101;
public static final String para