万商超信
阿里云短信验证码接口判断验证码是否正确(阿里云短信验证码接口)
                      2021-12-13 14:44
                    
                    公众号:不会写代码的阿P
整合Redis+springboot+阿里云的短信验证服务
一、阿里云的一些准备
1、 登录阿里云
2、进入AccessKey管理
3、添加用户组
4、添加短信服务的权限
5、创建用户
重要提示:保存你的AccessKey Secret!!!后面会用到
6、添加用户至用户组
7、注册阿里云的短信服务
提示:一些短信服务的费用可以略过
8、添加短信模板
9、添加短信签名
10、短信服务的API
链接:https://help.aliyun.com/product/44282.html?spm=5176.12212571.0.0.6bc71cbeKBnawP
11、短信服务的依赖和实例文档
实例文档:打开OpenAPI Explorer代码:
 import com.aliyuncs.CommonRequest;
 import com.aliyuncs.CommonResponse;
 import com.aliyuncs.DefaultAcsClient;
 import com.aliyuncs.IAcsClient;
 import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.exceptions.ServerException;
 import com.aliyuncs.http.MethodType;
 import com.aliyuncs.profile.DefaultProfile;
 /*
 pom.xml
 <dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <version>4.0.3</version>
 </dependency>
 */
 public class AddSmsSign {
     public static void main(String[] args) {
         DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
         IAcsClient client = new DefaultAcsClient(profile);
 
         CommonRequest request = new CommonRequest();
         request.setSysMethod(MethodType.POST);
         request.setSysDomain("dysmsapi.aliyuncs.com");
         request.setSysVersion("2017-05-25");
         request.setSysAction("AddSmsSign");
         request.putQueryParameter("RegionId", "cn-hangzhou");
         try {
             CommonResponse response = client.getCommonResponse(request);
             System.out.println(response.getData());
        } catch (ServerException e) {
             e.printStackTrace();
        } catch (ClientException e) {
             e.printStackTrace();
        }
    }
 }
二、整合springboot
1、导入依赖
 <!--阿里云的短信包-->
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
             <version>4.1.0</version>
         </dependency>
         <!--需要json字符串。[阿里云短信验证码接口判断验证码是否正确(阿里云短信验证码接口)]。导入json依赖-->
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.62</version>
         </dependency>
         <!--Redis-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
2、配置文件
 spring.redis.host=127.0.0.1
 spring.redis.port=6379
3、接口
 /**
      * 验证是否发送成功
      * @param phoneNum 手机号
      * @param templateCode 验证模板
      * @param code 验证码
      * @return
      */
     boolean send(String phoneNum, String templateCode, Map<String,Object> code);
4、servic层实现接口
 @Override
 public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
         //accessSecret 在你添加用户的时候会出现、就看你当时有没有保存了!!!!
         //1、连接阿里云
         DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GJny9Nw9328mJVuNbk9", "accessSecret");
         IAcsClient client = new DefaultAcsClient(profile);
         //2、请求
         CommonRequest request = new CommonRequest();
         //目前使用的最新依赖,所以需要把api换一下
         request.setMethod(MethodType.POST);
         request.setDomain("dysmsapi.aliyuncs.com");
         request.setVersion("2017-05-25");
         request.setAction("SendSms");
         //3、自定义参数格式《手机号、验证码、签名、模板》
         request.putQueryParameter("PhoneNumbers", phoneNum);            //阿里云短信服务的签名名称         request.putQueryParameter("SignName", 签名名称);
         request.putQueryParameter("TemplateCode", templateCode);
         //4、短信验证码
         request.putQueryParameter("TemplateParam", JSON.toJSONString(code));
         try {
             CommonResponse response = client.getCommonResponse(request);
             //判断验证是否发送成功
             return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
             e.printStackTrace();
        } catch (ClientException e) {
             e.printStackTrace();
        }
         return false;
    }
5、Controller层
 @Controller
 @CrossOrigin//解决跨域问题
 public class SendSmsApi {
 
     @Autowired
     private SendSms sendSms;
 
     @Autowired
     private RedisTemplate<String,String> redisTemplate;
 
     @GetMapping("/send/{phone}")
     @ResponseBody
     public String sendMsgCode(@PathVariable("phone") String phone){
         String code = redisTemplate.opsForValue().toString().substring(0,4);
         if (!StringUtils.isEmpty(code)){
             return phone+":"+code+"已存在未使用的验证码!!!";
        }
         //生成随机4位的验证码
         code = UUID.randomUUID().toString().substring(0,4);
         HashMap<String,Object> param = new HashMap<>();
         param.put("code",code);
 
         boolean isSend = sendSms.send(phone,"模版CODE",param);
         if (isSend){
             //验证码存在redis中
             redisTemplate.opsForValue().set(phone,code,5, TimeUnit.SECONDS);
             return phone+":"+code+"发送成功!!!";
        }else {
             return phone+":"+code+"发送失败!!!";
        }
    }
 }
 
测试:启动服务 http://localhost:8080/
请求地址:http://localhost:8080/send/电话号码
测试结果
此文章为原创!!!转载请注明出处

