1)新建几个文件夹

5. 模拟真实项目的短信验证 - 图1

2)编写接口 SendSmsService

  1. public interface SendSmsService {
  2. /**
  3. * 用于发送短信
  4. * @param phoneNumber :手机号
  5. * @param templateCode :模板编号
  6. * @param code :验证码
  7. * @return 是否发送成功
  8. */
  9. public boolean send(String phoneNumber, String templateCode, Map<String, Object> code);
  10. }

3)编写实现类 SendSmsServiceImpl

  1. public class SendSmsServiceImpl implements SendSmsService {
  2. @Override
  3. public boolean send(String phoneNumber, String templateCode, Map<String, Object> code) {
  4. DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
  5. IAcsClient client = new DefaultAcsClient(profile);
  6. // 构建请求
  7. CommonRequest request = new CommonRequest();
  8. request.setSysMethod(MethodType.POST);
  9. request.setSysDomain("dysmsapi.aliyuncs.com");
  10. request.setSysVersion("2017-05-25");
  11. request.setSysAction("SendSms");
  12. // 上面不需要改
  13. // 自定义参数 :
  14. // 手机号,这里 Value 就填用户的手机号,实际应用须要从表单获取
  15. request.putQueryParameter("PhoneNumbers", phoneNumber);
  16. // 签名,这里的 Value 就是在阿里云上申请的 签名
  17. request.putQueryParameter("SignName", "XXXX");
  18. // 模板,这里的 Value 就是在阿里云上申请的模板的 模版CODE 值
  19. request.putQueryParameter("TemplateCode", templateCode);
  20. // 验证码,真实应用需要自动构建验证码
  21. request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
  22. try {
  23. CommonResponse response = client.getCommonResponse(request);
  24. System.out.println(response.getData());
  25. // 自带的判断是否成功的方法
  26. return response.getHttpResponse().isSuccess();
  27. } catch (ServerException e) {
  28. e.printStackTrace();
  29. } catch (ClientException e) {
  30. e.printStackTrace();
  31. }
  32. return false;
  33. }
  34. }

4)编写配置文件,连接 Redis :application.yml

  1. server:
  2. port: 8088
  3. # 配置 Redis
  4. spring:
  5. redis:
  6. host: 192.168.142.120
  7. port: 6379
  8. password: 123456

5)编写调用接口

  1. @RestController
  2. @CrossOrigin // 跨域的支持
  3. public class SmsApiController {
  4. @Autowired
  5. private SendSmsService sendSmsService;
  6. @Autowired
  7. private RedisTemplate redisTemplate;
  8. @GetMapping("send/{phone}")
  9. public String sendSms(@PathVariable("phone") String phoneNumber){
  10. // 调用发送发放(模拟真实业务,整合 Redis)
  11. // 判断当前手机号是否存储在 Redis 中
  12. // 如果没有则发送短信
  13. // 如果有表示上一个验证码还未过期,不用发送
  14. String code = (String) redisTemplate.opsForValue().get(phoneNumber);
  15. if (!StringUtils.isEmpty(code)){
  16. return "[手机号: "+phoneNumber + "],[验证码: " + code +"],还未过期";
  17. }
  18. // 生成验证码
  19. code = UUID.randomUUID().toString().substring(0,6);
  20. HashMap<String, Object> map = new HashMap<>();
  21. map.put("code",code);
  22. sendSmsService.send(phoneNumber,"XXXX",map);
  23. // 如果发送成功,就放入 Redis
  24. if (isSend){
  25. redisTemplate.opsForValue().set(phoneNumber,code,5, TimeUnit.MINUTES);
  26. return "[手机号: "+phoneNumber + "],[验证码: " + code +"],发送成功";
  27. }else {
  28. return "[手机号: "+phoneNumber + "],[验证码: " + code +"],发送失败";
  29. }
  30. }
  31. }

6)使用浏览器访问测试!!