阿里云短信服务
1、开通阿里云短信服务获取“accessKeyId”,“secret”,“templateCode”等参数。
2、引入maven依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
3、创建工具类AliyunSmsUtil,代码如下:
public class AliyunSmsUtil {
/**
* @Desc: 发送短信验证码
* @Param: accessKeyId
* @Param: secret
* @Param: templateCode 短信模版CODE
* @Param: signname 短信签名
* @Param: tel 短信接收号码
* @Param: code 短信验证码
*/
public static boolean sendVcode(String accessKeyId, String secret, String templateCode, String signname, String tel, String code) {
DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, secret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "default");
request.putQueryParameter("PhoneNumbers", tel);
request.putQueryParameter("SignName", signname);
request.putQueryParameter("TemplateCode", templateCode);
JSONObject params = new JSONObject();
params.put("code", code);
request.putQueryParameter("TemplateParam", params.toJSONString());
try {
log.debug("=> send aliyun sms req:{}", JSON.toJSONString(request));
CommonResponse response = client.getCommonResponse(request);
log.debug("<= send aliyun sms resp:{}", response.getData());
JSONObject respJson = JSON.parseObject(response.getData());
if (null != respJson && "OK".equalsIgnoreCase(respJson.getString("Code"))) {
return true;
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return false;
}
}
4、验证码验证,取出redis中当前手机号的验证码,进行验证即可,通过后清除redis验证码。
上一篇: Python简单生成8位随机密码的方法
下一篇: Mybatis返回插入主键id的方法