• 在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.成功示例

  • 相关阅读:
    springboot酒店客房管理系统设计
    bug是什么意思详细介绍
    关于多对多关系(即E-R图中m:n)中的界面展示优化
    spring框架
    【数据结构】链表--单链表
    Stable Diffusion 秋葉aaaki整合包远程访问设置
    数据结构和算法(链表结构、栈、队列、递归行为、哈希表和有序表)(2)
    sparksql的SQL风格编程
    将CSV文件快速导入MySQL中
    Qt|多个窗口共有一个提示框类
  • 原文地址:https://blog.csdn.net/qq_53722480/article/details/138066103