阿里云的手机短信验证

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

  1. 依赖包:

    复制代码
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
             <version>4.6.0</version>
         </dependency>
  2. 实现代码:

    复制代码
     @Value("${aliyun.accessKeyId}")
     private String accessKeyId;
     @Value("${aliyun.accessKeySecret}")
     private String accessKeySecret;
     @Value("${aliyun.signName}")
     private String signName;
     @Value("${aliyun.templateCode}")
     private String templateCode;
     /**
      * 手机短信验证
      * @param phone
      * @param codeMap
      * @return
      */
     @Override
     public boolean phoneNote(String phone, Map<String, String> codeMap) {
          if (StrUtil.isBlank(phone)){
              return false;
          }
          //-------------------------阿里云-短信验证代码------------------
         //基本参数
         DefaultProfile profile =
                 DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
         IAcsClient client = new DefaultAcsClient(profile);
         //设置相关固定的参数
         CommonRequest request = new CommonRequest();
         //request.setProtocol(ProtocolType.HTTPS);
         request.setMethod(MethodType.POST);
         request.setDomain("dysmsapi.aliyuncs.com");
         request.setVersion("2017-05-25");//版本号-不要改
         request.setAction("SendSms");
         //设置发送相关的参数
         request.putQueryParameter("PhoneNumbers",phone); //手机号
         request.putQueryParameter("SignName",signName); //申请阿里云 签名名称
         request.putQueryParameter("TemplateCode",templateCode); //申请阿里云 模板code
         request.putQueryParameter("TemplateParam", JSONUtil.toJsonStr(codeMap)); //验证码数据,转换json数据传递
         //最终发送
         try {
             CommonResponse response = client.getCommonResponse(request);
             boolean success = response.getHttpResponse().isSuccess();
             log.warn("短信验证结果1="+success);
             return true; //成功
         } catch (ClientException e) {
             e.printStackTrace();
             return false;//失败
         }
    
     }

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

  1. 依赖包:

    复制代码
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>dysmsapi20170525</artifactId>
             <version>2.0.24</version>
         </dependency>
  2. 代码:

    复制代码
     /**
      * 手机短信验证
      * @param phone
      * @param codeMap
      * @return
      */
     @Override
     public boolean phoneNoteNew(String phone, Map<String, String> codeMap) throws Exception {
         //-------------------------阿里云-短信验证代码------------------
       Client client =client(accessKeyId,accessKeySecret);
         SendSmsRequest sendSmsRequest = new SendSmsRequest()
                 .setSignName(signName)
                 .setTemplateCode(templateCode)
                 .setPhoneNumbers(phone)
                 .setTemplateParam(JSONUtil.toJsonStr(codeMap));
        RuntimeOptions runtime = new RuntimeOptions();
         try {
             SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
              log.warn("短信验证结果=="+resp.getStatusCode());
             log.warn("短信验证结果22=="+resp.getBody().getMessage());
               return true;
         } catch (Exception e) {
             e.printStackTrace();
             return false;
         }
    
     }
     private Client client(String setAccessKeyId, String setAccessKeySecret) throws Exception {
         Config config = new Config()
                 // 必填,您的 AccessKey ID
                 .setAccessKeyId(setAccessKeyId)
                 // 必填,您的 AccessKey Secret
                 .setAccessKeySecret(setAccessKeySecret);
         // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
         config.endpoint = "dysmsapi.aliyuncs.com";
         return new Client(config);
     }

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

相关推荐
妙蛙种子3112 分钟前
【Java设计模式 | 创建者模式】 原型模式
java·开发语言·后端·设计模式·原型模式
Lyyaoo.9 分钟前
【JAVA基础面经】线程的状态
java·开发语言
Hello小赵9 分钟前
C语言如何自定义链接库——编译与调用
android·java·c语言
希望永不加班14 分钟前
SpringBoot 配置绑定:@ConfigurationProperties
java·spring boot·后端·spring
悟空码字15 分钟前
MySQL性能优化的天花板:10条你必须掌握的顶级SQL分析技巧
java·后端·mysql
indexsunny18 分钟前
互联网大厂Java面试实战:Spring Boot、MyBatis与Kafka在电商场景中的应用
java·spring boot·面试·kafka·mybatis·电商·技术栈
殷紫川22 分钟前
CompletableFuture 异步编程全解:核心能力、编排方案、异常处理与超时控制
java
ss27327 分钟前
致Java初学者的一封信
java·开发语言
white-persist27 分钟前
【vulhub spring CVE-2018-1270】CVE-2018-1270 Spring Messaging 远程命令执行漏洞 完整复现详细分析解释
java·服务器·网络·数据库·后端·python·spring
潇洒畅想35 分钟前
1.1 从∑到∫:用循环理解求和与累积
java·数据结构·python·算法