com.alibaba fastjson com.aliyun aliyun-java-sdk-core
- application.properties
# 服务端口 server.port=8005 # 服务名 spring.application.name=service-msm # mysql数据库连接 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.database= 0 spring.redis.timeout=1800000 spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=-1 #最大阻塞等待时间(负数表示没限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0 #最小空闲 #返回json的全局时间格式 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 #mybatis日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl3. 整合短信服务 3.1 生成随机数的工具类
package com.tjk.msmservice.utils;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class RandomUtil {
private static final Random random = new Random();
private static final DecimalFormat fourdf = new DecimalFormat("0000");
private static final DecimalFormat sixdf = new DecimalFormat("000000");
public static String getFourBitRandom() {
return fourdf.format(random.nextInt(10000));
}
public static String getSixBitRandom() {
return sixdf.format(random.nextInt(1000000));
}
public static ArrayList getRandom(List list, int n) {
Random random = new Random();
HashMap
3.2 发送短信
Service
public class MsmServiceImpl implements MsmService {
@Override
public boolean send(HashMap map, String phone) {
if(StringUtils.isEmpty(phone)) return false;
DefaultProfile profile =
DefaultProfile.getProfile("default", "Yourskey", "Yourkeysecret");
IAcsClient client = new DefaultAcsClient(profile);
//设置相关固定的参数
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
//设置发送相关的参数
request.putQueryParameter("PhoneNumbers",phone); //手机号
request.putQueryParameter("SignName","申请阿里云 签名名称"); //申请阿里云 签名名称
request.putQueryParameter("TemplateCode","模板code"); //申请阿里云 模板code
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map)); //验证码数据,转换json数据传递
try {
//最终发送
CommonResponse response = client.getCommonResponse(request);
boolean success = response.getHttpResponse().isSuccess();
return success;
}catch(Exception e) {
e.printStackTrace();
return false;
}
}
}
3.3 验证码5分钟内有效
设置redis的有效时间
- 从redis中获取验证码,如果有直接返回
- 如果没有就向阿里云发送请求
@RestController
@CrossOrigin
@RequestMapping("/edumsm/msm")
public class MsmController {
@Autowired
private MsmService msmService;
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("send/{phone}")
public R sendMsm(@PathVariable String phone){
//1. 从redis中获取验证码,如果有直接返回
String code = redisTemplate.opsForValue().get(phone);
if(!StringUtils.isEmpty(code)){
return R.ok();
}
//如果没有就向阿里云发送请求
//生成随机值,传递阿里云进行发送
code= RandomUtil.getFourBitRandom();
HashMap map = new HashMap<>();
map.put("code",code);
boolean isSend= msmService.send(map,code);
if(isSend){
//发送成功 把发送成功验证码放到redis里面
//设置有效时间
redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
return R.ok();
}else {
return R.error().message("短信发送失败");
}
}
}



