老司机午夜精品视频播放-老司机午夜精品视频观看-老司机午夜精品视频在线观看免费-老司机午夜精品网站在线观看-亚洲综合精品成人-亚洲综合精品成人啪啪

您所在的位置:主頁 > 短信問答 >

阿里云短信驗證碼接口判斷驗證碼是否正確(阿里云短信驗證碼接口)

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<StringObject> 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/電話號碼

測試結果


此文章為原創!!!轉載請注明出處




圖片展示

Copyright ? 2007-2020 萬商超信短信群發平臺  版權所有 Power by 備案號:粵ICP備20066710號-9

網站地圖 導航地圖