一、通过手机号获取验证码
创建Message实体
@Data
public class Message implements Serializable {
private static final long serialVersionUID = - 2167241494964834937 L;
private String mobile;
private String signName;
private String TemplateCode;
private String TemplateParam;
public Message ( String mobile, String signName, String templateCode, String templateParam) {
this . mobile = mobile;
this . signName = signName;
this . TemplateCode = templateCode;
this . TemplateParam = templateParam;
}
Controller层代码
@GetMapping ( "getCode" )
@ApiOperation ( value = "获取短信验证码" )
public AjaxResult getCode ( @RequestParam ( "phone" ) String phone) {
return AjaxResult. success ( wxUserService. getCode ( phone) ) ;
}
Service实现代码–获取验证存入redis
@Value ( "${message.signName}" )
private String signName;
@Value ( "${message.templateCode}" )
private String templateCode;
@Override
public int getCode ( String phone) {
int code = 0 ;
String param = SendMessageUtil. getSixNum ( ) ;
JSONObject paramJSON = new JSONObject ( ) ;
paramJSON. put ( "code" , param) ;
Message message = new Message ( phone, signName, templateCode, "" + paramJSON) ;
SendSmsResponse sendSmsResponse = null;
try {
sendSmsResponse = SendMessageUtil. sendSms ( message) ;
if ( "OK" . equalsIgnoreCase ( sendSmsResponse. getCode ( ) ) ) {
code = 200 ;
}
System. out. println ( sendSmsResponse. getMessage ( ) ) ;
} catch ( ClientException e) {
e. printStackTrace ( ) ;
}
if ( code == 200 ) {
redisCache. setCacheObject ( phone, param, Constants. CAPTCHA_EXPIRATION, TimeUnit. MINUTES) ;
return 0 ;
} else {
return - 1 ;
}
}
二、登陆验证
Controller层
@ApiOperation ( value = "登录" )
@PostMapping ( "login" )
public AjaxResult login ( @RequestBody WxLogin wxLogin) {
int i= wxUserService. login ( wxLogin) ;
if ( i!= 0 ) {
return AjaxResult. success ( "登陆成功" ) ;
}
return AjaxResult. error ( "验证码错误/验证码已过期" ) ;
}
Sercvice实现
@Override
public int login ( WxLogin wxLogin) {
String phone= wxLogin. getPhone ( ) ;
String code = redisCache. getCacheObject ( phone) ;
if ( code!= null) {
if ( code. equals ( wxLogin. getCode ( ) ) ) {
return 0 ;
}
}
return 1 ;
}
WxLogin实体
@Data
public class WxLogin {
* 手机号
* /
private String phone;
private String code;
}
SendMessageUtil工具类
@Component
public class SendMessageUtil {
private static String product;
@Value ( "${message.product}" )
public void setProduct ( String product) {
this . product = product;
}
private static String domain;
@Value ( "${message.domain}" )
public void setDomain ( String domain) {
this . domain = domain;
}
private static String accessKeyId;
@Value ( "${message.accessKeyId}" )
public void setAccessKeyId ( String accessKeyId) {
this . accessKeyId = accessKeyId;
}
private static String accessKeySecret;
@Value ( "${message.accessKeySecret}" )
public void setAccessKeySecret ( String accessKeySecret) {
this . accessKeySecret = accessKeySecret;
}
public static SendSmsResponse sendSms ( Message message) throws ClientException {
System. setProperty ( "sun.net.client.defaultConnectTimeout" , "10000" ) ;
System. setProperty ( "sun.net.client.defaultReadTimeout" , "10000" ) ;
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 ( message. getMobile ( ) ) ;
request. setSignName ( message. getSignName ( ) ) ;
request. setTemplateCode ( message. getTemplateCode ( ) ) ;
request. setTemplateParam ( message. getTemplateParam ( ) ) ;
SendSmsResponse sendSmsResponse = ( SendSmsResponse) acsClient. getAcsResponse ( request) ;
return sendSmsResponse;
}
public static String getSixNum ( ) {
String str = "0123456789" ;
StringBuilder sb = new StringBuilder ( 4 ) ;
for ( int i = 0 ; i < 6 ; ++ i) {
char ch = str. charAt ( ( new Random ( ) ) . nextInt ( str. length ( ) ) ) ;
sb. append ( ch) ;
}
return sb. toString ( ) ;
}
}
配置文件内容
#阿里云短信对接
message:
product: "Dysmsapi"
domain: "dysmsapi.aliyuncs.com" # 域名
accessKeyId: "" #用于标识用户。
accessKeySecret: "" #是用来验证用户的密钥
signName: "" #短信签名名称
templateCode: "" #短信模板ID