阿里云的手机短信验证

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

  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

相关推荐
凤凰院凶涛QAQ10 小时前
《Java版数据结构 & 集合类剖析》栈与队列:“push/pop 是栈的灵魂,offer/poll 是队列的骨架——四组 API,两种人生”
java·开发语言·数据结构
掘金_答案11 小时前
上线那天,一个 ConcurrentHashMap 差点送走我的 AI 客服——3 天排查 JVM 血泪史
java·后端·架构
猿与禅12 小时前
CosId 分布式 ID 生成器完全教程:从架构原理到生产落地
java·shardingsphere·雪花算法·分布式id·高性能·cosid·号段模式
MindUp12 小时前
企业网盘权限模型解析:多层级访问控制与审计能力选型指南
java·linux·运维
NG47712 小时前
【软件设计与体系结构】-策略设计模式
java·设计模式·软件工程
程序员天天困12 小时前
优雅记录操作日志:从注解到 SpEL 的全链路实践与开源方案对比
java·后端
战血石LoveYY13 小时前
Integer类型超限,导致数据异常
java·算法
乐观的Terry14 小时前
1、为什么要自己造发布系统
java·spring boot·后端·spring cloud·架构
兰令水14 小时前
hot100【acm版】【2026.7.13打卡-java版本】
java·开发语言·数据结构·算法·leetcode·面试
嗝屁小孩纸14 小时前
Maven版本升级&Nexus推送极简执行步骤
java·elasticsearch·maven