阿里云短信服务

https://www.aliyun.com/product/sms?spm=5176.159202.J_8058803260.68.64ae6a56APLp1H

工具类 HttpUtils

HttpUtils 请从 https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
或者直接下载:http://code.fegine.com/HttpUtils.zip
下载相应的依赖请参照 https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
相关jar包(非pom)直接下载:http://code.fegine.com/aliyun-jar.zip

相关配置

  1. spring:
  2. cloud:
  3. nacos:
  4. discovery:
  5. server-addr: 192.168.163.131:8848
  6. alicloud:
  7. sms:
  8. host: https://fesms.market.alicloudapi.com
  9. path: /sms/
  10. skin: 1
  11. sign: 175622
  12. appcode: 93b7e19861a24c519a7548b17dc16d75

发送验证码组件

  1. package com.zsy.third.party.component;
  2. import com.zsy.common.utils.HttpUtils;
  3. import lombok.Data;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.util.EntityUtils;
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.stereotype.Component;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. /**
  11. * @Description:
  12. * @author: zhangshuaiyin
  13. * @createTime: 2020-06-27 09:39
  14. **/
  15. @ConfigurationProperties(prefix = "spring.cloud.alicloud.sms")
  16. @Data
  17. @Component
  18. public class SmsComponent {
  19. private String host;
  20. private String path;
  21. private String skin;
  22. private String sign;
  23. private String appcode;
  24. public void sendCode(String phone, String code) {
  25. String method = "GET";
  26. Map<String, String> headers = new HashMap<>();
  27. // 最后在header中的格式(中间是英文空格)为 Authorization:APPCODE 93b7e19861a24c519a7548b17dc16d75
  28. headers.put("Authorization", "APPCODE " + appcode);
  29. Map<String, String> queries = new HashMap<String, String>();
  30. queries.put("code", code);
  31. queries.put("phone", phone);
  32. queries.put("skin", skin);
  33. queries.put("sign", sign);
  34. //JDK 1.8示例代码请在这里下载: http://code.fegine.com/Tools.zip
  35. try {
  36. HttpResponse response = HttpUtils.doGet(host, path, method, headers, queries);
  37. //System.out.println(response.toString());如不输出json, 请打开这行代码,打印调试头部状态码。
  38. //状态码: 200 正常;400 URL无效;401 appCode错误; 403 次数用完; 500 API网管错误
  39. //获取response的body
  40. System.out.println(EntityUtils.toString(response.getEntity()));
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }

测试

  1. @Test
  2. public void sendSmsCode() {
  3. smsComponent.sendCode("13838383838", "134531");
  4. }

发送短信接口

  1. /**
  2. * @author: zhangshuaiyin
  3. * @createTime: 2020-06-27 10:04
  4. **/
  5. @Controller
  6. @RequestMapping(value = "/sms")
  7. public class SmsSendController {
  8. @Resource
  9. private SmsComponent smsComponent;
  10. /**
  11. * 提供给别的服务进行调用
  12. * @param phone
  13. * @param code
  14. * @return
  15. */
  16. @GetMapping(value = "/sendCode")
  17. public R sendCode(@RequestParam("phone") String phone, @RequestParam("code") String code) {
  18. //发送验证码
  19. smsComponent.sendCode(phone,code);
  20. return R.ok();
  21. }
  22. }

接口防刷和验证码再次校验

mall-auth-server LoginController.java

  1. @ResponseBody
  2. @GetMapping(value = "/sms/sendCode")
  3. public R sendCode(@RequestParam("phone") String phone) {
  4. //1、接口防刷
  5. String redisCode = stringRedisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone);
  6. if (!StringUtils.isEmpty(redisCode)) {
  7. //活动存入redis的时间,用当前时间减去存入redis的时间,判断用户手机号是否在60s内发送验证码
  8. long currentTime = Long.parseLong(redisCode.split("_")[1]);
  9. if (System.currentTimeMillis() - currentTime < 60000) {
  10. //60s内不能再发
  11. return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(), BizCodeEnum.SMS_CODE_EXCEPTION.getMsg());
  12. }
  13. }
  14. //2、验证码的再次效验 redis.存key-phone,value-code
  15. int code = (int) ((Math.random() * 9 + 1) * 100000);
  16. String codeNum = String.valueOf(code);
  17. String redisStorage = codeNum + "_" + System.currentTimeMillis();
  18. //存入redis,防止同一个手机号在60秒内再次发送验证码
  19. stringRedisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone,
  20. redisStorage, 10, TimeUnit.MINUTES);
  21. thirdPartFeignService.sendCode(phone, codeNum);
  22. return R.ok();
  23. }