首先在腾讯云短信服务中创建签名
然后创建短信模板
导包
<!-- 腾讯云包--> <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java-sms</artifactId> <version>3.1.754</version> </dependency>
镜像文件
<repositories> <repository> <id>tencent</id> <name>tencent maven mirror</name> <url>https://mirrors.tencent.com/nexus/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
生成随机验证码的工具类
public class RandomUtil { private static final Random random = new Random(); private static final DecimalFormat fourdf = new DecimalFormat("0000"); private static final DecimalFormat sixdf = new DecimalFormat("000000"); //生成4位随机数 public static String getFourBitRandom() { return fourdf.format(random.nextInt(10000)); } //生成6位随机数 public static String getSixBitRandom() { return sixdf.format(random.nextInt(1000000)); } }
在yaml文件中配置
tencent: sms: #腾讯云短信服务参数 #腾讯云账户secretId,secretKey keyId: keysecret: #短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 signName: #模板 ID: 必须填写已审核通过的模板 ID templateId: #短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId smsSdkAppId:
发送验证码短信
/** * 发送验证码 * @param phoneNumber * @return */ public boolean send(String phoneNumber) { //判断手机号是否为空 if (StringUtils.isEmpty(phoneNumber)){ return false; } try{ // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密 // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取 Credential cred = new Credential(ConstantSmsUtils.SECRET_ID, ConstantSmsUtils.SECRET_KEY); // 实例化一个http选项,可选的,没有特殊需求可以跳过 HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("sms.tencentcloudapi.com"); // 实例化一个client选项,可选的,没有特殊需求可以跳过 ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); // 实例化要请求产品的client对象,clientProfile是可选的 第二个参数是地域信息 SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile); // 实例化一个请求对象,每个接口都会对应一个request对象 SendSmsRequest req = new SendSmsRequest(); //设置固定的参数 req.setSmsSdkAppid(ConstantSmsUtils.SMSSDKAPP_ID);// 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId req.setSign(ConstantSmsUtils.SIGN_NAME);//短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 req.setTemplateID(ConstantSmsUtils.TEMPLATE_ID);//模板 ID: 必须填写已审核通过的模板 ID /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */ // String sessionContext = "**"; // req.setSessionContext(sessionContext); //设置发送相关的参数 String[] phoneNumberSet1 = {"+86"+phoneNumber}; req.setPhoneNumberSet(phoneNumberSet1);//发送的手机号 //生成6位数随机验证码 String verificationCode = RandomUtil.getSixBitRandom(); String[] templateParamSet1 = {verificationCode,"2"};//模板的参数 第一个是验证码,第二个是过期时间 req.setTemplateParamSet(templateParamSet1);//发送验证码 //发送短信 // 返回的resp是一个SendSmsResponse的实例,与请求对象对应 SendSmsResponse resp = client.SendSms(req); System.out.println("resp"+resp); // 输出json格式的字符串回包 System.out.println(SendSmsResponse.toJsonString(resp)); //将验证码放入redis中 // redisService.setCacheObject(VERIFICATION_CODE + phoneNumber, verificationCode, 60*5L, TimeUnit.SECONDS); return true; } catch (TencentCloudSDKException e) { e.printStackTrace(); return false; } }