SpringBoot實現阿里雲簡訊介面對接的示例程式碼
阿新 • • 發佈:2020-09-28
前言
公司最近專案需要一個手機驗證碼的功能,任務確定後,倍感亞歷山大,以為和第三方對接的都好麻煩,查阿里的API、網上大神寫的部落格,各種查之後才發現,簡單的一塌糊塗,這裡想說個問題,不知道其他的攻城獅們是不是和我一樣的心裡,剛接觸個沒做過的任務時,會一臉懵裡的著急,無從下手的感覺,後來會了,就覺得簡單的一*,在這裡我說一下自己的體會,遇到任何難點,先理思路、任務拆分、逐個查資料,其實一套下來,就不會那種一臉懵逼的乾著急。。。
所需條件
1、阿里雲賬戶
2、開通雲通訊中的簡訊服務
3、申請簡訊簽名和模板
4、建立access_key和access_secret
5、然後就是程式碼編寫
話不囉嗦,直接開始開發步驟
開發步驟
開通簡訊服務
建立建立access_key和access_secret
申請簡訊模板和簽名
開發步驟
1、建立AliyunConfig類
package com.preread.user.config; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.Random; /** * @Description: 阿里雲簡訊介面配置類 * @author: yangxf * @date: 2019/4/11 15:01 */ public class AliyunConfig { /* 簡訊API產品名稱(簡訊產品名固定,無需修改) */ private static final String product = "Dysmsapi"; /* 簡訊API產品域名,介面地址固定,無需修改 */ private static final String domain = "dysmsapi.aliyuncs.com"; /* 此處需要替換成開發者自己的accessKeyId和accessKeySecret(在阿里雲訪問控制檯尋找) */ private static final String accessKeyId = "你的accessKeyId"; //TODO: 這裡要寫成你自己生成的 private static final String accessKeySecret = "你的accessKeySecret";//TODO: 這裡要寫成你自己生成的 /* 簡訊傳送 */ public static SendSmsResponse sendSms(String phone) throws ClientException { /* 超時時間,可自主調整 */ System.setProperty("sun.net.client.defaultConnectTimeout","10000"); System.setProperty("sun.net.client.defaultReadTimeout","10000"); /* 初始化acsClient,暫不支援region化 */ IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou","cn-hangzhou",product,domain); IAcsClient acsClient = new DefaultAcsClient(profile); /* 組裝請求物件-具體描述見控制檯-文件部分內容 */ SendSmsRequest request = new SendSmsRequest(); /* 必填:待發送手機號 */ request.setPhoneNumbers(phone); /* 必填:簡訊簽名-可在簡訊控制檯中找到 */ request.setSignName("提前看"); //TODO: 這裡是你簡訊簽名 /* 必填:簡訊模板code-可在簡訊控制檯中找到 */ request.setTemplateCode("你的模板code"); //TODO: 這裡是你的模板code /* 可選:模板中的變數替換JSON串,如模板內容為"親愛的使用者,您的驗證碼為$[code]"時,此處的值為 */ request.setTemplateParam("{\"code\":\"" + getMsgCode() + "\"}"); // hint 此處可能會丟擲異常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){ System.out.println("簡訊傳送成功!驗證碼:" + getMsgCode()); }else { System.out.println("簡訊傳送失敗!"); } return sendSmsResponse; } /** * @Function: 生成驗證碼 * @author: yangxf * @Date: 2019/4/11 15:30 */ private static String getMsgCode() { int n = 6; StringBuilder code = new StringBuilder(); Random ran = new Random(); for (int i = 0; i < n; i++) { code.append(Integer.valueOf(ran.nextInt(10)).toString()); } return code.toString(); } }
2、controller層呼叫
/** * @Function: 簡訊驗證介面 * @author: Yangxf * @Date: 2019/4/11 15:39 */ @RequestMapping("/smsverification") public Object SmsVerification(@Param("phone") String phone) { return userViewService.SmsVerification(phone); }
3、service層程式碼
/** * @Function: 簡訊驗證 * @author: Yangxf * @Date: 2019/4/11 15:56 * @param: phone 手機號 */ @Override public Map<String,Object> SmsVerification(String phone) { Map<String,Object> map = new HashMap<>(); try { AliyunConfig.sendSms(phone); map.put("code",200); map.put("msg","簡訊驗證傳送成功"); return map; } catch (ClientException e) { map.put("code",300); map.put("msg",e.getMessage()); return map; } }
4、整合阿里雲SDK
<!-- 阿里雲簡訊SDK --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.7</version> </dependency>
至此程式碼階段OK,可以測試了
效果如下:
到此這篇關於SpringBoot實現阿里雲簡訊介面對接的示例程式碼的文章就介紹到這了,更多相關SpringBoot 阿里雲簡訊介面對接內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!