springboot整合阿⾥短信服务1.导⼊阿⾥短信服务sdk
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
sms:
accessId: 填⾃⼰真实的配置
accessKey: 填⾃⼰真实的配置
signName: 填⾃⼰真实的配置
codeTemplate: 填⾃⼰真实的配置
product: Dysmsapi
domain: dysmsapi.aliyuncs
3.整合SmsService
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.del.v20170525.SendSmsRequest;
import com.del.v20170525.SendSmsResponse;
import ptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.dis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
springmvc面试题 阿里
import java.util.Random;
import java.util.UUID;
@Service
public class SmsService {
@Value("${sms.accessId}")
private String accessId;
@Value("${sms.accessKey}")
private String accessKey;
@Value("${sms.signName}")
private String signName;
@Value("${deTemplate}")
private String codeTemplate;
@Value("${sms.product}")
private String product;    //短信API产品名称(短信产品名固定,⽆需修改)
@Value("${sms.domain}")
private String domain;      //dysmsapi.aliyuncs
@Autowired
private RedisService redisService;(有问题私聊我)
/**
* 根据⽤户输⼊的phone发送验证码
* @param phone 电话号码
*/
public void sendSmsCode(String phone){
if(!phone.matches("^1[3|4|5|7|8][0-9]{9}$")){
System.out.println("⼿机号码格式不正确");
return;
}
//判断⽤户输⼊的电话号码是否频繁发送
if(isSendOfen(phone)){
System.out.println("频繁,请稍后再试");
return;
}
Sms sms = makeCode(phone);      //制作验证码,6位随机数字
JSONObject smsJson=new JSONObject();
smsJson.put("code",Code());
smsJson.put("product","Dysmsapi");
SendSmsResponse sendSmsResponse=null;
try {
sendSmsResponse = send(phone,signName,codeTemplate,smsJson);
} catch (ClientException e) {
e.printStackTrace();
System.out.println("短信验证码发送失败");
return;
}
Code() != null && Code().equals("OK")) {
//短信发送成功,将短信记录到redis中
redisCode(sms);
System.out.println("短信发送成功");
}
}
//将验证码缓存到redis中,10分钟过后⾃动清除该缓存
private void redisCode(Sms sms) {
redisService.Phone(),10L,sms);
}
//随机⽣成6位数的短信码
private Sms makeCode(String phone) {
Random random = new Random();
StringBuilder code = new StringBuilder();
for(int i=0;i<6;i++){
int next =Int(10);
code.append(next);
}
return new Sms(String(),System.currentTimeMillis());
}
//判断验证功发送时候频繁
private boolean isSendOfen(String phone) {
(phone)==null) {
return false;
}else{
//判断上⼀次记录的时间和当前时间进⾏对⽐,如果两次相隔时间⼩于120s,视为短信发送频繁
Sms (phone,Sms.class);
//两次中间⾄少有2分钟的间隔时间
Time()+120*1000>=System.currentTimeMillis()) {
return true;
}
return false;
}
}
/**
* 验证短信
* @param phone
* @param code
* @return
*/
public boolean validSmsCode(String phone, String code){
//取出所有有关该⼿机号的短信验证码
(phone)==null){
System.out.println("短信验证失败");
return false;
}
Sms (phone,Sms.class);
if (Code().equals(code)){
System.out.println("短信验证成功");
//删除掉该redis
redisService.delete(phone);
return true;
}
return false;
}
/
**
* 发信
* @param phone
* @param signName
* @param templateCode
* @param params
* @return
* @throws ClientException
*/
SendSmsResponse send(String phone, String signName, String templateCode, JSONObject params) throws ClientException {
//初始化ascClient,暂时不⽀持多region(请勿修改)
IClientProfile profile = Profile("cn-hangzhou", accessId,
accessKey);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
SendSmsRequest request = new SendSmsRequest();
//使⽤post提交
request.setMethod(MethodType.POST);
//必填:待发送⼿机号。⽀持以逗号分隔的形式进⾏批量调⽤,批量上限为1000个⼿机号码,批量调⽤相对于单条调⽤及时性稍有延迟,验证码类型的短信推荐使⽤单条调⽤的⽅式        request.setPhoneNumbers(phone);
//必填:短信签名-可在短信控制台中到
request.setSignName(signName);
//必填:短信模板-可在短信控制台中到
request.setTemplateCode(templateCode);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
//友情提⽰:如果JSON中需要带换⾏符,请参照标准的JSON协议对换⾏符的要求,⽐如短信内容中包含\r\n的情况在JSON中需要表⽰成\\r\\n,否则会导致JSON在服务端解析失败        request.JSONString());
request.setOutId(UUID.randomUUID().toString());
//请求失败这⾥会抛ClientException异常
AcsResponse(request);
}
}
4.sMs.java
public class Sms {
private String phone;  //电话号码
private String code;    //短信验证码
private Long time;      //短信验证码⽣成时间
public Sms() {
}
public Sms(String phone, String code, Long time) {
this.phone = phone;
this.time = time;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCode() {
return code;
}
public void setCode(String code) {
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
}
  转:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。