• 在Spring Boot应用中实现阿里云短信功能的整合


    1.程序员必备程序网站

    天梦星服务平台 (tmxkj.top)icon-default.png?t=N7T8https://tmxkj.top/#/

    2.导入坐标

    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>aliyun-java-sdk-core</artifactId>
    4. <version>4.5.0</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.aliyun</groupId>
    8. <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
    9. <version>1.0.0</version>
    10. </dependency>

    3.yml文件配置

    1. aliyun:
    2. sms: #阿里云发短信
    3. access-key-id: "********" #角色的id
    4. access-key-secret: "******" #角色的密匙
    5. sign-name: "天梦星科技"
    6. template-code: "*******"

    4.核心代码

    1. @Component
    2. public class SmsUtil {
    3. @Value("${aliyun.sms.access-key-id}")
    4. private String accessKeyId;
    5. @Value("${aliyun.sms.access-key-secret}")
    6. private String accessKeySecret;
    7. @Value("${aliyun.sms.sign-name}")
    8. private String signName;
    9. @Value("${aliyun.sms.template-code}")
    10. private String templateCode;
    11. public Result sendSms(String phone, String code) throws ClientException {
    12. Result result = new Result();
    13. IAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret));
    14. CommonRequest request = new CommonRequest();
    15. request.setSysMethod(MethodType.POST);
    16. request.setSysDomain("dysmsapi.aliyuncs.com");
    17. request.setSysVersion("2017-05-25");
    18. request.setSysAction("SendSms");
    19. request.putQueryParameter("RegionId", "cn-hangzhou");
    20. request.putQueryParameter("PhoneNumbers", phone);
    21. request.putQueryParameter("SignName", signName);
    22. request.putQueryParameter("TemplateCode", templateCode);
    23. request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
    24. CommonResponse response = client.getCommonResponse(request);
    25. if (JSON.parseObject(response.getData()).get("Message").equals("OK")){
    26. result.setCode(200);
    27. result.setMsg("短信发送成功");
    28. }else {
    29. result.setCode(400);
    30. result.setMsg(JSON.parseObject(response.getData()).get("Message"));
    31. }
    32. return result;
    33. }
    34. }

     

    1. public class ToolUtil {
    2. /**
    3. * 判断是否是手机号
    4. * @param phoneNumber
    5. * @return
    6. */
    7. public static boolean isPhoneNumber(String phoneNumber) {
    8. String regex = "^1[3-9]\\d{9}$";
    9. return Pattern.matches(regex, phoneNumber);
    10. }
    11. /**
    12. * 获取长度为 5 的随机数字
    13. * @return 随机数字
    14. * 用途短信验证码
    15. */
    16. public static String getSmsRandomNumber() {
    17. char[] nonceChars = new char[5]; //指定长度为6/自己可以要求设置
    18. for (int index = 0; index < nonceChars.length; ++index) {
    19. nonceChars[index] = SYMBOLS2.charAt(RANDOM.nextInt(SYMBOLS2.length()));
    20. }
    21. return new String(nonceChars);
    22. }
    23. }

     

    5.调用测试

    1. /**
    2. * 发送短信验证嘛
    3. */
    4. @GetMapping("/sendMessage")
    5. public Result sendMessage(@RequestParam("phone") String phone){
    6. Result result = new Result();
    7. String code = getSmsRandomNumber(); // 生成随机验证码
    8. try {
    9. if(isPhoneNumber(phone)){
    10. //redisDao.vSet(code,code,imaileEpxtime);
    11. result = smsUtil.sendSms(phone, code);
    12. }else {
    13. result.setCode(400);
    14. result.setMsg("手机号格式错误!");
    15. }
    16. } catch (ClientException e) {
    17. e.fillInStackTrace();
    18. result.setCode(500);
    19. result.setMsg(e.getErrMsg());
    20. }
    21. return result;
    22. }

    6.成功示例

  • 相关阅读:
    文心一言 Python编程之
    Ubuntu20.04 中已经安装 Pytorch 但 Import 报错 - 解决记录
    PyTorch学习笔记(一)
    Java版本spring cloud + spring boot企业电子招投标系统源代码
    华为数通方向HCIP-DataCom H12-821题库(单选题:301-320)
    java基于springboot的学生公寓管理系统
    面霸的自我修养:ThreadLocal专题
    【新书推荐】用户画像:了解用户,助力企业成长 ——《用户画像:平台构建与业务实践》
    PHP-Composer包开发、发布流程
    java毕业生设计医院疫情防控管理系统计算机源码+系统+mysql+调试部署+lw
  • 原文地址:https://blog.csdn.net/qq_53722480/article/details/138066103