pom
<dependencies>
<!--阿里云短信-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
<!--常见的工具类-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.9</version>
</dependency>
<!--Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
controller
@RestController
@CrossOrigin
public class RedisTes {
@Autowired
private SendSms sendSms;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@GetMapping("/send/{phone}")
public String code(@PathVariable("phone") String phone){
// 调用发送方法(Redis)
String code = redisTemplate.opsForValue().get(phone);
if (!StringUtils.isEmpty(code)){
return phone + ": "+ code + " 已存在,还没有过期";
}
// 生成随机的验证码到Redis
code = UUID.randomUUID().toString().substring(0,4);
HashMap<String, Object> map = new HashMap<>();
map.put("code", code);
// 申请的验证码只能为数字,templateCode为阿里云的模板code序列号
boolean isSend = sendSms.send(phone, "SMS", map);
if (isSend){
redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.SECONDS);
return phone + ": " + code + "发送成功! ";
}else {
return " 发送失败";
}
}
}
service
public interface SendSms {
/**
* 输入 发送的手机号码、模板短信code、发送的信息code
* @param phoneNum
* @param templateCode
* @param code
* @return
*/
public boolean send(String phoneNum, String templateCode, Map<String,Object> code);
}
serviceImpl
@Service
public class SendSmsImpl implements SendSms{
@Override
public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
// 连接阿里云
DefaultProfile profile = DefaultProfile.getProfile("地区", "身份令牌key", "身份令牌pwd");
IAcsClient client = new DefaultAcsClient(profile);
// 构建请求
// 构建请求
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phoneNum);//接收短信的手机号码
request.setSignName("ElloeStudy");//短信签名名称
request.setTemplateCode(templateCode);//短信模板CODE
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
return true;
}catch (ClientException e) {
e.printStackTrace();
}catch (ClientException e){
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
return false;
}
}
标签:code,短信,SpringBoot,redis,boot,phone,return,public,String
From: https://www.cnblogs.com/ElloeStudy/p/17603482.html