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("手机号格式不对请从新输入手机号");
}
}



