栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot如何去集成腾讯云的短信服务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot如何去集成腾讯云的短信服务

1、先去申请一个微信公众号,通过微信公众号,以个人的名义去申请腾讯短信服务。

2、 填写你注册公众号要注册成啥类型的,如果是自己玩或者自己测试的话我推荐大家呢去注册这个订阅号他这个呢比较好注册,然后去填写自己的相关的信息。

3注册成功了,就会生成这个界面

4、然后我们去设置公众号的设置里面去把他的那个隐私设置给打开 5、 去腾讯云的短信服务哪里去免费注册一个(默认呢腾讯云是送的客户100条的免费的短信发送个数)。  6、然后单击,免费试用,进去了以后呢他左侧的选项栏里面呢有一个这个快速入门,点击创建自己的签名以及自己的短信模板去申请去给腾讯工作人员去申请。

7、 点击概括这个里面有人家官方给你写好的ipA文档

 8、导入腾讯云的依赖
    
        
            com.tencentcloudapi
            tencentcloud-sdk-java
            3.1.270
        
     
            com.alibaba
            fastjson
            1.2.28
        

 

9 、编写java代码,这里呢我是自己封装了一个工具包。
package com.baidu.util;

import com.alibaba.fastjson.JSONObject;
import com.baidu.config.configuration;
import com.baidu.entity.Request;
import com.baidu.entity.SendStatusSet;
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.v20210111.SmsClient;
// 导入要请求接口对应的request response类
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;

import static com.baidu.config.configuration.sdkAppId;
import static com.baidu.config.configuration.signName;

public class SendSmsUtil {
    //腾讯短信验证
    
public static Boolean sendsms(String phone,String sixBitRandom) throws TencentCloudSDKException {
    
    Credential cred = new Credential(configuration.secretId, configuration.secretKey);
    // 实例化一个http选项,可选,没有特殊需求可以跳过
    HttpProfile httpProfile = new HttpProfile();
    // 设置代理(无需要直接忽略)
    // httpProfile.setProxyHost("真实代理ip");
    // httpProfile.setProxyPort(真实代理端口);
    
    httpProfile.setReqMethod(configuration.request);
    
    httpProfile.setConnTimeout(60);
    
    httpProfile.setEndpoint(configuration.territory);
    
    ClientProfile clientProfile = new ClientProfile();
    
    clientProfile.setSignMethod("HmacSHA256");
    clientProfile.setHttpProfile(httpProfile);
    
    SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
    
    SendSmsRequest req = new SendSmsRequest();
    
    
    req.setSmsSdkAppId(sdkAppId);
    req.setSignName(signName);
    req.setTemplateId(configuration.templateId);
    
    String[] templateParamSet = {sixBitRandom};
    req.setTemplateParamSet(templateParamSet);
    
    
    final StringBuilder builder = new StringBuilder();
    builder.append("+86");
    builder.append(phone);
    String[] phoneNumberSet = {builder.toString()};
    req.setPhoneNumberSet(phoneNumberSet);
    
    String sessionContext = "";
    req.setSessionContext(sessionContext);
    
    String extendCode = "";
    req.setExtendCode(extendCode);

    

    
    SendSmsResponse res = client.SendSms(req);
    // 输出json格式的字符串回包
    
    final String s = SendSmsResponse.toJsonString(res);
    final JSONObject object = JSONObject.parseObject(s);
    final Request request = JSONObject.toJavaObject(object, Request.class);
    final List sendStatusSet = request.getSendStatusSet();
    String code=null;
    for (SendStatusSet statusSet : sendStatusSet) {
        statusSet.setPhoneNumber("*****");
       code=statusSet.getCode();
    }
    if (code.equals("Ok")){
        return true;
    }else {
        return false;
    }
    // 也可以取出单个值,你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
    // System.out.println(res.getRequestId());
    
}
    
    private static boolean matchPhoneNumber(String phoneNumber) {
        String regex = "^1\d{10}$";
        if(phoneNumber==null||phoneNumber.length()<=0){
            return false;
        }
        return Pattern.matches(regex, phoneNumber);
    }
    //验证
    public static void main(String[] args) throws TencentCloudSDKException {
        final String sixBitRandom = RandomUtil.getSixBitRandom();
        String phone="139352099";
        final boolean b = SendSmsUtil.matchPhoneNumber(phone);
        if (b){
            if(SendSmsUtil.sendsms(phone, sixBitRandom)){
                System.out.println("发送成功");
            }else {
                System.out.println("发送失败");
            }
        }else {
            System.out.println("请从新输入手机号");
        }
    }

}
10 、我这里呢我是把我的这个公共的配置属性都提取出来了,或者呢大家呢也可以把这个配置属性呢放到我们的这个application.properties里面已value(“${xx}”)的这个方式呢去取值,这里的secretID 跟sercretKey呢可以去腾讯去设置登录 - 腾讯云
package com.baidu.config;

public interface configuration {
    //id
    String secretId="*****";
    //key
    String secretKey="*****";
    //请求方式
    String request="POST";
    //地域
    String territory="sms.tencentcloudapi.com";
    // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
    String sdkAppId="1400670952";
    
    // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
    String signName = "****";
    
    // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
    String templateId = "****";

}

 

 11、 生成随机数并测试(我这里呢生成的是6位的随机验证码)
public class RandomUtil {
//生成随机验证码
    private static final Random random = new Random();
    //我定义的验证码位数是6位
    private static final DecimalFormat sixdf = new DecimalFormat("000000");

    public static String getSixBitRandom() {
        return sixdf.format(random.nextInt(1000000));
    }
//校验手机号格式 因为呢手机号格式比较多然后我们这里呢采用的是这个手机号位数的校验
 private static boolean matchPhoneNumber(String phoneNumber) {
        String regex = "^1\d{10}$";
        if(phoneNumber==null||phoneNumber.length()<=0){
            return false;
        }
//判断正则表达式与我输入的值的长度是否相同
        return Pattern.matches(regex, phoneNumber);
    }
}
12、 编写实体类接收返回的json格式参数
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Request {
    //请求id
    private String RequestId;
    //发送状态集
    private List SendStatusSet;
}
package com.baidu.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
//发送状态集
public class SendStatusSet {
    //序列号
    private String SerialNo;
    //电话号码
    private String PhoneNumber;
    //费用
    private Integer  Fee;
    //上下文
    private String SessionContext;
    //标识
    private String Code;
    //消息
    private String Message;
    //异码
    private String IsoCode;
}

 

 13 、调用方法完成测试
  //验证
    public static void main(String[] args) throws TencentCloudSDKException {
        final String sixBitRandom = RandomUtil.getSixBitRandom();
        String phone="139352099";
        final boolean b = RandomUtil.matchPhoneNumber(phone);
        if (b){
            if(SendSmsUtil.sendsms(phone, sixBitRandom)){
                System.out.println("发送成功");
            }else {
                System.out.println("发送失败");
            }
        }else {
            System.out.println("手机号格式不对请从新输入手机号");
        }
    }

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号