• 阿里云的手机短信验证


    一,老版的手机短信验证:

    1. 依赖包:
    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>aliyun-java-sdk-core</artifactId>
    4. <version>4.6.0</version>
    5. </dependency>
    1. 实现代码:
    1. @Value("${aliyun.accessKeyId}")
    2. private String accessKeyId;
    3. @Value("${aliyun.accessKeySecret}")
    4. private String accessKeySecret;
    5. @Value("${aliyun.signName}")
    6. private String signName;
    7. @Value("${aliyun.templateCode}")
    8. private String templateCode;
    9. /**
    10. * 手机短信验证
    11. * @param phone
    12. * @param codeMap
    13. * @return
    14. */
    15. @Override
    16. public boolean phoneNote(String phone, Map<String, String> codeMap) {
    17. if (StrUtil.isBlank(phone)){
    18. return false;
    19. }
    20. //-------------------------阿里云-短信验证代码------------------
    21. //基本参数
    22. DefaultProfile profile =
    23. DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
    24. IAcsClient client = new DefaultAcsClient(profile);
    25. //设置相关固定的参数
    26. CommonRequest request = new CommonRequest();
    27. //request.setProtocol(ProtocolType.HTTPS);
    28. request.setMethod(MethodType.POST);
    29. request.setDomain("dysmsapi.aliyuncs.com");
    30. request.setVersion("2017-05-25");//版本号-不要改
    31. request.setAction("SendSms");
    32. //设置发送相关的参数
    33. request.putQueryParameter("PhoneNumbers",phone); //手机号
    34. request.putQueryParameter("SignName",signName); //申请阿里云 签名名称
    35. request.putQueryParameter("TemplateCode",templateCode); //申请阿里云 模板code
    36. request.putQueryParameter("TemplateParam", JSONUtil.toJsonStr(codeMap)); //验证码数据,转换json数据传递
    37. //最终发送
    38. try {
    39. CommonResponse response = client.getCommonResponse(request);
    40. boolean success = response.getHttpResponse().isSuccess();
    41. log.warn("短信验证结果1="+success);
    42. return true; //成功
    43. } catch (ClientException e) {
    44. e.printStackTrace();
    45. return false;//失败
    46. }
    47. }

    二,新版的手机短信验证:

    1. 依赖包:
    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>dysmsapi20170525</artifactId>
    4. <version>2.0.24</version>
    5. </dependency>
    1. 代码:
    1. /**
    2. * 手机短信验证
    3. * @param phone
    4. * @param codeMap
    5. * @return
    6. */
    7. @Override
    8. public boolean phoneNoteNew(String phone, Map<String, String> codeMap) throws Exception {
    9. //-------------------------阿里云-短信验证代码------------------
    10. Client client =client(accessKeyId,accessKeySecret);
    11. SendSmsRequest sendSmsRequest = new SendSmsRequest()
    12. .setSignName(signName)
    13. .setTemplateCode(templateCode)
    14. .setPhoneNumbers(phone)
    15. .setTemplateParam(JSONUtil.toJsonStr(codeMap));
    16. RuntimeOptions runtime = new RuntimeOptions();
    17. try {
    18. SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
    19. log.warn("短信验证结果=="+resp.getStatusCode());
    20. log.warn("短信验证结果22=="+resp.getBody().getMessage());
    21. return true;
    22. } catch (Exception e) {
    23. e.printStackTrace();
    24. return false;
    25. }
    26. }
    27. private Client client(String setAccessKeyId, String setAccessKeySecret) throws Exception {
    28. Config config = new Config()
    29. // 必填,您的 AccessKey ID
    30. .setAccessKeyId(setAccessKeyId)
    31. // 必填,您的 AccessKey Secret
    32. .setAccessKeySecret(setAccessKeySecret);
    33. // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
    34. config.endpoint = "dysmsapi.aliyuncs.com";
    35. return new Client(config);
    36. }

    关注,收藏,点赞,有问题可以私信“门主” :v:z13135361785 

  • 相关阅读:
    七、运算符
    lua基本语法
    复数go版本的下载和管理
    Android硬件服务访问(2):Driver
    如何基于先进视频技术,构建互联网视频监控安全管理平台解决方案
    自己动手实现 HashMap(Python字典),彻底系统的学习哈希表(上篇)——不看血亏!!!
    【Cherno的C++视频】Static analysis in C++
    传奇外网开服教程-GEE传奇外网全套架设教程
    面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
    【密码学篇】商用密码产品的密钥体系结构小结
  • 原文地址:https://blog.csdn.net/m0_55699184/article/details/133644739