阿里云的手机短信验证

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

  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

相关推荐
杨丰玮41820 分钟前
暑假Java知识点回顾:类与对象知识总结
java·开发语言
盐焗鹌鹑蛋21 分钟前
【Linux】ELF与库加载
java·linux·服务器
WarPigs26 分钟前
Unity未激活的物体无法响应事件?
java·unity·游戏引擎
十五喵源码网30 分钟前
基于SpringBoot2+vue2的公寓报修管理系统
java·毕业设计·springboot·论文笔记
珍珠先生32 分钟前
第16章 集合框架:List 与 Set
java
薛定猫AI1 小时前
【深度解析】DeepSeek Harness:可插拔智能体运行时如何连接大模型与真实计算机
java·运维·网络
rannn_1111 小时前
【力扣hot100】回溯专题|全排列、子集、字母组合、组合总和、括号生成、单词搜索、分割回文串、N皇后
java·算法·leetcode·回溯
nxb5561 小时前
云原生:控制器
java·开发语言·云原生
咖啡八杯1 小时前
统一返回对象设计:AjaxResult 与 R 的取舍
java·spring boot·框架·若依·返回值
小手cool2 小时前
JSON.parseObject 返回 List 时不能强制转换的解决方案
java·json·list