我们在日常开发中,比如登录,验证许多都需要对手机号进行发送短信验证码等,今天就实现一个腾讯云sms短信服务,只需要注册腾讯云输入短信模板,短信内容,个人信息就可以向手机号发送短信以及信息如图所示:
具体实现代码如下:
0.加依赖
<!--发送短信-->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.770</version>
</dependency>
<!--添加国内公共仓库-->
<repositories>
<repository>
<!-- 阿里-->
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
<!--腾讯-->
<repository>
<id>tencent</id>
<name>tencent maven mirror</name>
<url>https://mirrors.tencent.com/nexus/repository/maven-public/</url>
</repository>
</repositories>
1.书写yml文件(首先需要在腾讯云平台上创建一个短信应用,并获取相应的API密钥(API Key)和API密钥密文(API Secret)。这些信息将用于身份验证和 API 调用,还需要涉及到短信模板和签名的设置。短信模板是用来定义短信的内容和格式的,而签名是用来说明短信发送的来源和真实性的。网址:短信_文本短信_通知短信_营销短信_验证码短信 - 腾讯云)
之后根据注册的服务去配置yml(不知道怎么获取的可以看视频或者问chatGPT):
tencent:
#指定就近地域域名
endpoint: sms.tencentcloudapi.com
#密钥ID
secretId: ##自己的秘钥ID
#密钥
secretKey: ##自己的秘钥
#行为ID
appId: ##自己的行为ID
#地理位置
region: ap-nanjing ##离自己最近的腾讯机房
#签名内容
sign-name: "大白猫" ##发送短信的开始企业名称
#模板ID
templateId: ##自己的模版ID
2.之后在写一个配置文件获取该yml的信息放到该类中加载带bean容器
@Data
@Configuration
@ConfigurationProperties(prefix = "tencent")
public class SmsProperties {
/**
*指定就近地域域名
* 就是腾讯机房离哪最近
*/
private String endpoint;
/**
* 密钥ID
*/
private String secretId;
/**
* 密钥
*/
private String secretKey;
/**
* 行为ID
*/
private String appId;
/**
* 地理位置
*/
private String region;
/**
* 签名内容
*/
private String signName;
/**
* 模板ID
*/
private String templateId;
}
3.之后创建工具类
@Component
public class SmsUtil {
@Resource
SmsProperties properties;
public Boolean sendSms(String mobile, String code) {
try {
//创建了一个名为cred的Credential对象,该对象用于存储身份认证信息,包括密钥ID和密钥
Credential cred = new Credential(properties.getSecretId(), properties.getSecretKey());
// 实例化一个http选项,可选,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.setReqMethod("POST");
httpProfile.setEndpoint(properties.getEndpoint());
//创建了一个ClientProfile对象clientProfile,并设置签名方法为"HmacSHA256",并将上一步创建的httpProfile设置到其中
ClientProfile clientProfile = new ClientProfile();
clientProfile.setSignMethod("HmacSHA256");
clientProfile.setHttpProfile(httpProfile);
//SmsClient是一个用于发送短信的客户端类
SmsClient client = new SmsClient(cred, properties.getRegion(), clientProfile);
///创建一个短信请求体
SendSmsRequest req = new SendSmsRequest();
// 行为ID
req.setSmsSdkAppId(properties.getAppId());
// 签名内容
req.setSignName(properties.getSignName());
// 模板ID
req.setTemplateId(properties.getTemplateId());
String[] templateParamSet = {code};
req.setTemplateParamSet(templateParamSet);
//下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
//示例如:+861234567890,前面有一个+号 ,86为国家码,1234567890为手机号,最多不要超过200个手机号
//下面为手机号码
String[] phoneNumberSet = {mobile};
//放入短信请求体
req.setPhoneNumberSet(phoneNumberSet);
// 发送短信
SendSmsResponse res = client.SendSms(req);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
最后进行controller控制器接口的编写:
@RestController
@RequestMapping("sms")
@Api(tags = "短信控制器")
public class SmsServlet {
//注入短信工具类
@Resource SmsUtil smsUtil;
//注入redis工具类
@Resource
private RedisUtil redisUtil;
@PostMapping("send")
@ApiOperation("发送短信,随机6位数字")
@ApiImplicitParams({@ApiImplicitParam(name = "mobile", value = "要发送到的手机号")})
public R sendSms(String mobile) {
// 生成验证码
String random = RandomUtil.randomNumbers(6);
if (smsUtil.sendSms(mobile, random)) {
// 验证码发送给到redis
redisUtil.setString("mobile:" + mobile, random, 60);
return R.ok("短信发送成功");
}
return R.fail("短信发送失败");
}
@PostMapping("text")
@ApiOperation("发送短信")
@ApiImplicitParams({@ApiImplicitParam(name = "mobile", value = "要发送到的手机号"),@ApiImplicitParam(name = "text", value = "要发送的信息")})
public R sendSms1(String mobile,String text) {
//短信信息最大12位
if (smsUtil.sendSms(mobile, text)) {
// 验证码发送给到redis
redisUtil.setString("mobile:" + mobile, text, 60);
return R.ok("短信发送成功");
}
return R.fail("短信发送失败");
}
}
最后就可以实现对指定手机号发送短信,信息等