今天分享一个SpringBoot集成腾讯云短信的功能,平常除了工作,很多xdm做自己的小项目都可能用到短信,但自己去看文档挺费劲的,我这边就帮你节省时间,直接把步骤给你列出来,照做就行。
<!-- 腾讯云短信 --> <!--请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下--> <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java</artifactId> <version>3.1.714</version> </dependency>2)、新增properties配置
package com.imooc.utils; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @Data @PropertySource("classpath:tencentCloud.properties") @ConfigurationProperties(prefix = "tencent.cloud") public class TencentCloudProperties { // 堆代码 duidaima.com private String secretId; private String secretKey; }4)、短信工具类
package com.imooc.utils; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.sms.v20210111.SmsClient; import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class SMSUtils { @Autowired private TencentCloudProperties tencentCloudProperties; public void sendSMS(String phone, String code) throws Exception { try { /* 必要步骤: * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。 * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。 * 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人, * 以免泄露密钥对危及你的财产安全。 * CAM密匙查询获取: https://console.cloud.tencent.com/cam/capi */ Credential cred = new Credential(tencentCloudProperties.getSecretId(), tencentCloudProperties.getSecretKey()); // 实例化一个http选项,可选的,没有特殊需求可以跳过 HttpProfile httpProfile = new HttpProfile(); // httpProfile.setReqMethod("POST"); // 默认使用POST /* * SDK会自动指定域名。通常是不需要特地指定域名的,但是如果你访问的是金融区的服务 * 则必须手动指定域名,例如sms的上海金融区域名: sms.ap-shanghai-fsi.tencentcloudapi.com */ httpProfile.setEndpoint("sms.tencentcloudapi.com"); // 实例化一个client选项 ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); // 实例化要请求产品的client对象,clientProfile是可选的 SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile); // 实例化一个请求对象,每个接口都会对应一个request对象 SendSmsRequest req = new SendSmsRequest(); String[] phoneNumberSet1 = { "+86" + phone }; //电话号码 req.setPhoneNumberSet(phoneNumberSet1); req.setSmsSdkAppId("xxx"); // 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId req.setSignName("Java分享XX"); // 签名 req.setTemplateId("xxx"); // 模板id:必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 /* 模板参数(自定义占位变量): 若无模板参数,则设置为空 */ String[] templateParamSet1 = { code }; 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()); } } }5)、测试效果
@Autowired private SMSUtils smsUtils; @GetMapping("sms") public Object sendSMS() throws Exception { smsUtils.sendSMS("159xxxxxxxx", "6752"); return "sendSMS OK!"; }