由于阿里云短信现在不对个人客户开放,所以就进行了整合腾讯云短信
腾讯云整合和视频中逻辑有些差别,网上的教程有限,参考了别人的代码,最大限度的还原视频中的代码
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--腾讯云短信-->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-common</artifactId>
<version>3.1.488</version>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.209</version>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId>
<version>3.1.488</version>
</dependency>
<!-- 配置文件处理器 -->
<!--让自定义的配置在application.yaml进行自动提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
application.yml
tencentcloud:
sms:
secretId:
secretKey:
#短信应用 ID
appId:
#短信签名内容
sign:
#短信模板ID
templateID:
从配置文件读取常量
@Getter //idea2020.2.3版配置文件自动提示需要这个
@Setter
@Component
@ConfigurationProperties(prefix="tencentcloud.sms")
public class SmsProperties implements InitializingBean {
private String secretId;
private String secretKey;
private String appid;
private String sign;
private String templateID;
private static String SECRET_ID;
private static String SECRET_KEY;
private static String APP_ID;
private static String TEMPLATE_ID;
private static String SIGN;
@Override
public void afterPropertiesSet() throws Exception {
SECRET_ID=secretId;
SECRET_KEY=secretKey;
APP_ID=appid;
SIGN=sign;
TEMPLATE_ID=templateID;
}
}
common-util中引入工具类:RandomUtils.java、FormUtils.java
1、创建controller
@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理")
@CrossOrigin //跨域
@Slf4j
public class ApiSmsController {
@Autowired
private SmsService smsService;
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("send/{mobile}")
public R getCode(@PathVariable String mobile) throws ClientException {
//校验手机号是否合法
if(StringUtils.isEmpty(mobile) || !FormUtils.isMobile(mobile)){
log.error("请输入正确的手机号码 ");
throw new GuliException(ResultCodeEnum.LOGIN_PHONE_ERROR);
}
//生成验证码
String checkCode= RandomUtils.getFourBitRandom();
//发送验证码
smsService.send(mobile, checkCode);
redisTemplate.opsForValue().set(mobile,checkCode, 5, TimeUnit.MINUTES);
return R.ok().message("短信发送成功");
}
}
接口:SmsService.java
package com.atguigu.guli.service.sms.service;
import com.netflix.client.ClientException;
public interface SmsService {
void send(String mobile, String checkCode) throws ClientException;
}
接口:SmsServiceImpl.java
package com.atguigu.guli.service.sms.service.impl;
import com.atguigu.guli.common.base.result.ResultCodeEnum;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.sms.service.SmsService;
import com.atguigu.guli.service.sms.util.SmsProperties;
import com.netflix.client.ClientException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
@Autowired
private SmsProperties smsProperties;
@Override
public void send(String mobile, String checkCode) throws ClientException {
String jointMobile="+86"+mobile;
String secretId = smsProperties.getSecretId();
String secretKey = smsProperties.getSecretKey();
String appid = smsProperties.getAppid();
String sign = smsProperties.getSign();
String templateID = smsProperties.getTemplateID();
String[] phoneNumbers = { jointMobile};
//模板参数: 若无模板参数,则设置为空
String[] templateParams = { checkCode };//对应模板中{1}
try {
//必要步骤: 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey
Credential cred = new Credential(secretId , secretKey);
ClientProfile clientProfile = new ClientProfile();
//SDK 默认用 TC3-HMAC-SHA256 进行签名 非必要请不要修改该字段
clientProfile.setSignMethod("HmacSHA256");
// 实例化 SMS 的 client 对象 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量
SmsClient client = new SmsClient(cred, "", clientProfile);
//实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数 您可以直接查询 SDK 源码确定接口有哪些属性可以设置
SendSmsRequest req = new SendSmsRequest();
// 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666
req.setSmsSdkAppid(appid);
// 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息
req.setSign(sign);
//短信模板 ID: 必须填写已审核通过的模板 ID,可登录 [短信控制台] 查看模板 ID
req.setTemplateID(templateID);
//下发手机号码,采用 e.164 标准,+[国家或地区码][手机号] 例如+8613711112222
req.setPhoneNumberSet(phoneNumbers);
req.setTemplateParamSet(templateParams);
// 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的 返回的 res 是一个SendSmsResponse 类的实例,与请求对象对应
SendSmsResponse res = client.SendSms(req);
//获取响应结果
SendStatus[] sendStatusSet = res.getSendStatusSet();
log.info("短信发送返回的响应:"+sendStatusSet);
} catch (TencentCloudSDKException e) {
log.error("腾讯云短信发送sdk调用失败:"+e.getErrorCode()+","+e.getMessage());
throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR); }
}
}