Spring中使用騰訊云發(fā)送短信驗證碼的實現(xiàn)示例
1. 所需依賴
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.313</version>
</dependency>
2. 騰訊云配置
(1). 獲取短信簽名
在騰訊云控制臺中找到短信



我使用網(wǎng)站創(chuàng)建簽名
需要有域名, 且域名已完成備案

下面這個圖是網(wǎng)站備案號, 在騰訊云控制臺搜索網(wǎng)站備案即可找到

創(chuàng)建成功
記下 SignName

(2). 創(chuàng)建正文模板


模板隨便選一個即可, 其中的{1} {2}是參數(shù), 后來配置需要

我選擇的第一個, 一個參數(shù)

成功后, 記下TemplateId

(3). 創(chuàng)建密鑰

記錄密鑰 SecredId 和 SecretKey

(4). 獲取SdkAppId

3. 代碼
public class SmsServiceTencentSmsImpl {
public void send(String mobile, String message) { // 參數(shù)是電話號碼和發(fā)送的內容
try {
Credential cred = new Credential(你的SecredId, 你的SecredKey);
// 實例化一個http選項,可選的,沒有特殊需求可以跳過
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("sms.tencentcloudapi.com");
// 實例化一個client選項,可選的,沒有特殊需求可以跳過
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 實例化要請求產(chǎn)品的client對象,clientProfile是可選的
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
// 實例化一個請求對象,每個接口都會對應一個request對象
SendSmsRequest req = new SendSmsRequest();
String[] phoneNumberSet1 = {"+86" + mobile};
req.setPhoneNumberSet(phoneNumberSet1);
req.setSmsSdkAppId(你的SdkAppId);
req.setSignName(你的SignName);
req.setTemplateId(你的TemplateId);
String[] templateParamSet1 = {message}; // 你的正文模板參數(shù), 我的是一個, 如果兩個數(shù)組里兩個元素
req.setTemplateParamSet(templateParamSet1);
// 返回的resp是一個SendSmsResponse的實例,與請求對象對應
SendSmsResponse resp = client.SendSms(req);
// 輸出json格式的字符串回包
System.out.println(SendSmsResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}
到此這篇關于Spring中使用騰訊云發(fā)送短信驗證碼的實現(xiàn)示例的文章就介紹到這了,更多相關Spring 騰訊云發(fā)送短信驗證碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot實現(xiàn)短信驗證碼校驗方法思路詳解
- Spring Security Oauth2.0 實現(xiàn)短信驗證碼登錄示例
- SpringBoot + SpringSecurity 短信驗證碼登錄功能實現(xiàn)
- SpringBoot+Security 發(fā)送短信驗證碼的實現(xiàn)
- Spring Security 實現(xiàn)短信驗證碼登錄功能
- SpringSceurity實現(xiàn)短信驗證碼登陸
- springboot短信驗證碼登錄功能的實現(xiàn)
- Spring?Security短信驗證碼實現(xiàn)詳解
- SpringBoot發(fā)送短信驗證碼的實例
相關文章
Spring?Data?JPA命名約定查詢實現(xiàn)方法
這篇文章主要為大家介紹了Spring?Data?JPA命名約定查詢實現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Spring?Cloud?Gateway編碼實現(xiàn)任意地址跳轉
這篇文章主要介紹了Spring?Cloud?Gateway編碼實現(xiàn)任意地址跳轉的相關資料,需要的朋友可以參考下2023-06-06
spring?Cloud微服務阿里開源TTL身份信息的線程間復用
這篇文章主要為大家介紹了spring?Cloud微服務中使用阿里開源TTL身份信息的線程間復用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01

