美文网首页
Springboot集成腾讯短信验证码

Springboot集成腾讯短信验证码

作者: _Gaara_ | 来源:发表于2020-09-01 16:23 被阅读0次

搜索腾讯短信验证码,购买套餐。
进入控制台创建签名以及短信模板。都有指引操作,很容易。


模板申请

然后在项目里,添加如下依赖

       <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.122</version>
        </dependency>
  • 参数说明:
    PhoneNumberSet:目标手机号
    TemplateID: 申请的模板id
    SmsSdkAppid:短信控制台应用的id
    申请腾讯的secretKey和secretId,传送门

使用如下代码即可使用腾讯短信系统


package com.gaara.common;

import com.gaara.service.MessageCodeService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

//导入可选配置类
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

// 导入 SMS 模块的 client
import com.tencentcloudapi.sms.v20190711.SmsClient;

// 导入要请求接口对应的 request response 类
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/********************************
 *    Author Gaara              *
 *    Version 1.0               *
 *    @ClassName SendTencentMessage
 *    @Date 2020/9/1 下午3:51     
 *    @Description TODO         *
 ********************************/
@Component
public class SendTencentMessage {

    @Autowired
    private MessageCodeService service;

    public String sendMessage(int messageType,String phoneNumber) {
        String secretId = "";
        String secretKey = "";
        String code = service.sendCode();
        String result = "";
        try{

            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "", clientProfile);

            String params = "{\"PhoneNumberSet\":[\"+86"+phoneNumber+"\"],\"TemplateID\":\""+messageType+"\",\"Sign\":\"你的签名名称\",\"TemplateParamSet\":[\""+你的验证码+"\"],\"SmsSdkAppid\":\"在这里填id\"}";
            SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);

            SendSmsResponse resp = client.SendSms(req);

            result = SendSmsResponse.toJsonString(resp);

        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return result;
        /*try {
            *//* 必要步骤:
             * 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey
             * 本示例采用从环境变量读取的方式,需要预先在环境变量中设置这两个值
             * 您也可以直接在代码中写入密钥对,但需谨防泄露,不要将代码复制、上传或者分享给他人
             * CAM 密钥查询:https://console.cloud.tencent.com/cam/capi*//*
            Credential cred = new Credential(secretId, secretKey);

            // 实例化一个 http 选项,可选,无特殊需求时可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setConnTimeout(60);
            *//* SDK 会自动指定域名,通常无需指定域名,但访问金融区的服务时必须手动指定域名
             * 例如 SMS 的上海金融区域名为 sms.ap-shanghai-fsi.tencentcloudapi.com *//*
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            *//* 非必要步骤:
             * 实例化一个客户端配置对象,可以指定超时时间等配置 *//*
            ClientProfile clientProfile = new ClientProfile();
            *//* SDK 默认用 TC3-HMAC-SHA256 进行签名
             * 非必要请不要修改该字段 *//*
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            *//* 实例化 SMS 的 client 对象
             * 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量 *//*
            SmsClient client = new SmsClient(cred, "",clientProfile);
            *//* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
             * 您可以直接查询 SDK 源码确定接口有哪些属性可以设置
             * 属性可能是基本类型,也可能引用了另一个数据结构
             * 推荐使用 IDE 进行开发,可以方便地跳转查阅各个接口和数据结构的文档说明 *//*
            SendSmsRequest req = new SendSmsRequest();

            *//* 填充请求参数,这里 request 对象的成员变量即对应接口的入参
             * 您可以通过官网接口文档或跳转到 request 对象的定义处查看请求参数的定义
             * 基本类型的设置:
             * 帮助链接:
             * 短信控制台:https://console.cloud.tencent.com/smsv2
             * sms helper:https://cloud.tencent.com/document/product/382/3773 *//*

            *//* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666 *//*
            String appid = "";
            req.setSmsSdkAppid(appid);

            *//* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 *//*
           *//* String sign = "签名内容";
            req.setSign(sign);*//*


            *//* 模板 ID: 必须填写已审核通过的模板 ID,可登录 [短信控制台] 查看模板 ID *//*
            String templateID = messageType+"";
            req.setTemplateID(templateID);

            *//* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
             * 例如+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*//*
            String[] phoneNumbers = {"+86"+phoneNumber};
            req.setPhoneNumberSet(phoneNumbers);

            *//* 模板参数: 若无模板参数,则设置为空*//*
            *//*String[] templateParams = {"5678"};
            req.setTemplateParamSet(templateParams);*//*

            *//* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
             * 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 *//*
            SendSmsResponse res = client.SendSms(req);

            // 输出 JSON 格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(res));

            // 可以取出单个值,您可以通过官网接口文档或跳转到 response 对象的定义处查看返回字段的定义
            System.out.println(res.getRequestId());

        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }*/
    }
}

相关文章

网友评论

      本文标题:Springboot集成腾讯短信验证码

      本文链接:https://www.haomeiwen.com/subject/pimvsktx.html