springboot 腾讯云短信平台SDK
生活随笔
收集整理的這篇文章主要介紹了
springboot 腾讯云短信平台SDK
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在購買的騰訊云中短信控制臺上申請簽名和正文模板,審核通過后才可以使用
引入騰訊云sdkpom
<dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><version>3.1.62</version><!-- 注:這里只是示例版本號,請獲取并替換為 最新的版本號 --> </dependency> package com.example.ylmframeworkregister.common.sms; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.sms.v20190711.SmsClient; import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest; import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse; import com.tencentcloudapi.sms.v20190711.models.SendStatus; import java.util.regex.Matcher; import java.util.regex.Pattern;/*** 騰訊模板*/public class TencentSms {private String secretId;private String secretKey;private String appId;private String sign;public TencentSms(String secretId, String secretKey, String appId, String sign) {this.secretId = secretId;this.secretKey = secretKey;this.appId = appId;this.sign = sign;}/*** 發(fā)送短信* @param mobile* @param code*/public boolean sendSms(String mobile,String code) {try {if (!isMobile(mobile)) {return false;}/* 必要步驟:* 實例化一個認(rèn)證對象,入?yún)⑿枰獋魅腧v訊云賬戶密鑰對 secretId 和 secretKey* 本示例采用從環(huán)境變量讀取的方式,需要預(yù)先在環(huán)境變量中設(shè)置這兩個值* 您也可以直接在代碼中寫入密鑰對,但需謹(jǐn)防泄露,不要將代碼復(fù)制、上傳或者分享給他人/Credential cred = new Credential(secretId, secretKey);/* 實例化 SMS 的 client 對象* 第二個參數(shù)是地域信息,可以直接填寫字符串 ap-guangzhou,或者引用預(yù)設(shè)的常量 */SmsClient client = new SmsClient(cred, "");/* 實例化一個請求對象,根據(jù)調(diào)用的接口和實際情況,可以進(jìn)一步設(shè)置請求參數(shù)* 您可以直接查詢 SDK 源碼確定接口有哪些屬性可以設(shè)置* 屬性可能是基本類型,也可能引用了另一個數(shù)據(jù)結(jié)構(gòu)* 推薦使用 IDE 進(jìn)行開發(fā),可以方便地跳轉(zhuǎn)查閱各個接口和數(shù)據(jù)結(jié)構(gòu)的文檔說明 */SendSmsRequest req = new SendSmsRequest();/* 填充請求參數(shù),這里 request 對象的成員變量即對應(yīng)接口的入?yún)? 您可以通過官網(wǎng)接口文檔或跳轉(zhuǎn)到 request 對象的定義處查看請求參數(shù)的定義* 基本類型的設(shè)置:* 幫助鏈接://* 短信應(yīng)用 ID: 在 [短信控制臺] 添加應(yīng)用后生成的實際 SDKAppID,例如1400006666 */req.setSmsSdkAppid(appId);/* 短信簽名內(nèi)容: 使用 UTF-8 編碼,必須填寫已審核通過的簽名,可登錄 [短信控制臺] 查看簽名信息 */req.setSign(sign);/* 模板 ID: 必須填寫已審核通過的模板 ID,可登錄 [短信控制臺] 查看模板 ID */String templateID = "";req.setTemplateID(templateID);/* 下發(fā)手機(jī)號碼,采用 e.164 標(biāo)準(zhǔn),+[國家或地區(qū)碼][手機(jī)號]* 例如+8613711112222, 其中前面有一個+號 ,86為國家碼,13711112222為手機(jī)號,最多不要超過200個手機(jī)號*/String[] phoneNumbers = {"+86" + mobile};req.setPhoneNumberSet(phoneNumbers);/* 模板參數(shù): 若無模板參數(shù),則設(shè)置為空,如果不使用驗證碼功能注掉*/ /* String[] templateParams = {code};req.setTemplateParamSet(templateParams);*//* 通過 client 對象調(diào)用 SendSms 方法發(fā)起請求。注意請求方法名與請求對象是對應(yīng)的* 返回的 res 是一個 SendSmsResponse 類的實例,與請求對象對應(yīng) */SendSmsResponse res = client.SendSms(req);SendStatus[] sendStatusSet = res.getSendStatusSet();if (sendStatusSet != null && sendStatusSet.length == 1) {if ("OK".equalsIgnoreCase(sendStatusSet[0].getCode())) {return true;}}} catch (TencentCloudSDKException e) {e.printStackTrace();}return false;}private static Pattern p = Pattern.compile("^[1][0,1,2,3,4,5,7,8,9][0-9]{9}$"); // 驗證手機(jī)號public static boolean isMobile(String str) {boolean b = false;Matcher m = p.matcher(str);b = m.matches();return b;} } package com.example.ylmframeworkregister.config; import com.example.ylmframeworkregister.common.sms.TencentSms; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** 在這里編寫說明,對應(yīng)參數(shù)在配置文件添加**/ @Configuration public class CommonConfig {@Value("${tencent.sms.secret-id}")private String smsSecretId;@Value("${tencent.sms.secret-key}")private String smsSecretKey;@Value("${tencent.sms.app-id}")private String smsAppId;@Value("${tencent.sms.sign}")private String smsSign;@Beanpublic TencentSms tencentSms() {return new TencentSms(smsSecretId, smsSecretKey,smsAppId,smsSign);} }配置文件配置騰訊云賬戶密鑰對,短信應(yīng)用 ID,短信簽名內(nèi)容
tencent:sms:secret-id:*********secret-key: *******app-id: ****sign: ******可以根據(jù)自己需求更改,該文檔是發(fā)送手機(jī)驗證碼
@PostMapping(value = "/public/mobile") public Response sendSms(@RequestBody LoginMobileReqVo loginMobileReqVo) {logger.info("手機(jī)號發(fā)送手機(jī)號請求參數(shù){}", JSON.toJSONString(loginMobileReqVo));String mobile = loginMobileReqVo.getMobile();String code = RandomUtils.genDigitalCode(4);boolean isSuccess = tencentSms.sendSms(mobile, code);if (!isSuccess) {return Response.error("發(fā)送短信失敗");}return Response.ok(); }?
總結(jié)
以上是生活随笔為你收集整理的springboot 腾讯云短信平台SDK的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Librtmp的交叉编译详解
- 下一篇: JEECG框架心得