springboot 腾讯云短信平台SDK
·
在购买的腾讯云中短信控制台上申请签名和正文模板,审核通过后才可以使用
引入腾讯云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;
}
/**
* 发送短信
* @param mobile
* @param code
*/
public boolean sendSms(String mobile,String code) {
try {
if (!isMobile(mobile)) {
return false;
}
/* 必要步骤:
* 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey
* 本示例采用从环境变量读取的方式,需要预先在环境变量中设置这两个值
* 您也可以直接在代码中写入密钥对,但需谨防泄露,不要将代码复制、上传或者分享给他人
/
Credential cred = new Credential(secretId, secretKey);
/* 实例化 SMS 的 client 对象
* 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量 */
SmsClient client = new SmsClient(cred, "");
/* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
* 您可以直接查询 SDK 源码确定接口有哪些属性可以设置
* 属性可能是基本类型,也可能引用了另一个数据结构
* 推荐使用 IDE 进行开发,可以方便地跳转查阅各个接口和数据结构的文档说明 */
SendSmsRequest req = new SendSmsRequest();
/* 填充请求参数,这里 request 对象的成员变量即对应接口的入参
* 您可以通过官网接口文档或跳转到 request 对象的定义处查看请求参数的定义
* 基本类型的设置:
* 帮助链接:
/
/* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666 */
req.setSmsSdkAppid(appId);
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 */
req.setSign(sign);
/* 模板 ID: 必须填写已审核通过的模板 ID,可登录 [短信控制台] 查看模板 ID */
String templateID = "";
req.setTemplateID(templateID);
/* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
* 例如+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
String[] phoneNumbers = {"+86" + mobile};
req.setPhoneNumberSet(phoneNumbers);
/* 模板参数: 若无模板参数,则设置为空,如果不使用验证码功能注掉*/
/* String[] templateParams = {code};
req.setTemplateParamSet(templateParams);*/
/* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
* 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
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}$"); // 验证手机号
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;
/**
* 在这里编写说明,对应参数在配置文件添加
*
*/
@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;
@Bean
public TencentSms tencentSms() {
return new TencentSms(smsSecretId, smsSecretKey,smsAppId,smsSign);
}
}
配置文件配置腾讯云账户密钥对,短信应用 ID,短信签名内容
tencent:
sms:
secret-id:*********
secret-key: *******
app-id: ****
sign: ******
可以根据自己需求更改,该文档是发送手机验证码
@PostMapping(value = "/public/mobile")
public Response sendSms(@RequestBody LoginMobileReqVo loginMobileReqVo) {
logger.info("手机号发送手机号请求参数{}", JSON.toJSONString(loginMobileReqVo));
String mobile = loginMobileReqVo.getMobile();
String code = RandomUtils.genDigitalCode(4);
boolean isSuccess = tencentSms.sendSms(mobile, code);
if (!isSuccess) {
return Response.error("发送短信失败");
}
return Response.ok();
}


更多推荐
所有评论(0)