Spring Boot中快速集成腾讯云短信功能-Spring专区论坛-技术-SpringForAll社区

Spring Boot中快速集成腾讯云短信功能

前言

今天分享一个SpringBoot集成腾讯云短信的功能,平常除了工作,很多xdm做自己的小项目都可能用到短信,但自己去看文档挺费劲的,我这边就帮你节省时间,直接把步骤给你列出来,照做就行。

实战

1、申请密钥及签名模板

首先,要使用腾讯云短信,你得先在腾讯云有个账号,申请密钥及签名模板。

1)、找到访问管理-API密钥管理,勿泄漏。

 

d2b5ca33bd20231103014859

2)、签名及模板

要申请,推荐用公众号,描述中写几句赞美腾讯云的话,几分钟后就可以过审了。

 

d2b5ca33bd20231103014916

d2b5ca33bd20231103014924

3)、应用SDK APPID

d2b5ca33bd20231103014939

 

4)、短信工具类

应用ID、签名、模板id都从上面找到后改为自己的就行了。

图片[5]-Spring Boot中快速集成腾讯云短信功能-Spring专区论坛-技术-SpringForAll社区

d2b5ca33bd20231103015003

 

2、代码集成

腾讯云短信官方文档:https://cloud.tencent.com/document/product/382/43194


1)、引入依赖

一般在common模块中引入即可

<!-- 腾讯云短信 -->
<!--请到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配置

一般这种第三方接入的配置使用properties较好,和yml配置做区分。密钥参考前面的说明。

图片[7]-Spring Boot中快速集成腾讯云短信功能-Spring专区论坛-技术-SpringForAll社区

d2b5ca33bd20231103015029

 

3)、新增配置类

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 {

    private String secretId;
    private String secretKey;

}

4)、短信工具类

从官网拷过来修改即可,记得修改其中的应用ID、签名、模板id。

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)、测试效果

在服务中写一个方法测试即可,然后启动网关和user服务,访问 http://127.0.0.1:8000/u/sms 等一会儿就有短信通知了。

@Autowired
private SMSUtils smsUtils;

@GetMapping("sms")
public Object sendSMS() throws Exception {

    smsUtils.sendSMS("159xxxxxxxx", "6752");

    return "sendSMS OK!";
}

总结

集成第三方的短信接口其实很简单,费时间的主要是申请一些东西,以及阅读接口文档。

大家如果想省事,按照我的步骤来就行,接入个短信功能,也花不了什么钱。既可以体验下接入方式,也可以为自己的小项目增加一些亮点。

来源:https://www.cnblogs.com/fulongyuanjushi/p/17797860.html

请登录后发表评论

    没有回复内容