mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
!25 腾讯云&阿里云短信移除sdk,改为api实现
Merge pull request !25 from Richard/dev-2.x 贡献者:Richard
This commit is contained in:
commit
a7fdb952bc
@ -1,10 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
<packaging>pom</packaging>
|
||||
<name>sms4j</name>
|
||||
<description>sms_aggregation</description>
|
||||
@ -55,16 +55,16 @@
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
<properties>
|
||||
<unisms.version>0.0.4</unisms.version>
|
||||
<okhttp.version>3.14.9</okhttp.version>
|
||||
<aliyun.version>2.0.23</aliyun.version>
|
||||
<json.version>2.0.15</json.version>
|
||||
<forest.version>1.5.30</forest.version>
|
||||
<spring.boot.version>2.7.10</spring.boot.version>
|
||||
<hutool.version>5.8.16</hutool.version>
|
||||
<okhttp.version>3.14.9</okhttp.version>
|
||||
<tencent.version>3.1.622</tencent.version>
|
||||
<revision>1.0.5</revision>
|
||||
<revision>2.0.1</revision>
|
||||
<unisms.version>0.0.4</unisms.version>
|
||||
<spring.boot.version>2.7.10</spring.boot.version>
|
||||
<forest.version>1.5.30</forest.version>
|
||||
<jdcloud.version>1.3.3</jdcloud.version>
|
||||
<aliyun.version>2.0.23</aliyun.version>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
@ -83,67 +83,67 @@
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-spring-boot-starter</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-autoimmit</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-core</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-comm</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-api</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-aliyun</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-tencent</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-unisms</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-yunpian</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-huawei</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-jdcloud</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-cloopen</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-emay</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
@ -268,10 +268,6 @@
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-release-plugin</artifactId>
|
||||
<version>2.5.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.2.2</version>
|
||||
|
||||
@ -18,18 +18,6 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- 阿里云短信依赖-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-comm</artifactId>
|
||||
|
||||
@ -23,4 +23,22 @@ public class AlibabaConfig extends BaseConfig {
|
||||
*/
|
||||
@Builder.Default
|
||||
private String requestUrl = "dysmsapi.aliyuncs.com";
|
||||
|
||||
/**
|
||||
* 接口名称
|
||||
*/
|
||||
@Builder.Default
|
||||
private String action = "SendSms";
|
||||
|
||||
/**
|
||||
* 接口版本号
|
||||
*/
|
||||
@Builder.Default
|
||||
private String version = "2017-05-25";
|
||||
|
||||
/**
|
||||
* 地域信息默认为 cn-hangzhou
|
||||
*/
|
||||
@Builder.Default
|
||||
private String regionId = "cn-hangzhou";
|
||||
}
|
||||
|
||||
@ -1,16 +1,14 @@
|
||||
package org.dromara.sms4j.aliyun.config;
|
||||
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import org.dromara.sms4j.aliyun.service.AlibabaSmsImpl;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.aliyun.service.AlibabaSmsImpl;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
|
||||
|
||||
/**
|
||||
* AlibabaSmsConfig
|
||||
* <p> 阿里巴巴对象建造者
|
||||
*
|
||||
* @author :Wind
|
||||
* 2023/4/8 14:54
|
||||
**/
|
||||
@ -21,33 +19,18 @@ public class AlibabaSmsConfig {
|
||||
|
||||
private static AlibabaSmsConfig alibabaSmsConfig;
|
||||
|
||||
private Client client(AlibabaConfig alibabaConfig){
|
||||
try {
|
||||
Config config = new Config()
|
||||
// AccessKey ID
|
||||
.setAccessKeyId(alibabaConfig.getAccessKeyId())
|
||||
// AccessKey Secret
|
||||
.setAccessKeySecret(alibabaConfig.getAccessKeySecret());
|
||||
// 访问的域名
|
||||
config.endpoint = alibabaConfig.getRequestUrl();
|
||||
return new Client(config);
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage());
|
||||
throw new SmsBlendException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getAlibabaSms
|
||||
* getAlibabaSms
|
||||
* <p> 建造一个短信实现对像
|
||||
*
|
||||
* @author :Wind
|
||||
*/
|
||||
*/
|
||||
public static AlibabaSmsImpl createAlibabaSms(AlibabaConfig alibabaConfig) {
|
||||
if (alibabaSmsConfig == null){
|
||||
if (alibabaSmsConfig == null) {
|
||||
alibabaSmsConfig = new AlibabaSmsConfig();
|
||||
}
|
||||
if (alibabaSms == null){
|
||||
alibabaSms = new AlibabaSmsImpl(alibabaSmsConfig.client(alibabaConfig),
|
||||
if (alibabaSms == null) {
|
||||
alibabaSms = new AlibabaSmsImpl(
|
||||
alibabaConfig,
|
||||
BeanFactory.getExecutor(),
|
||||
BeanFactory.getDelayedTime());
|
||||
@ -56,17 +39,18 @@ public class AlibabaSmsConfig {
|
||||
}
|
||||
|
||||
/**
|
||||
* refresh
|
||||
* refresh
|
||||
* <p> 刷新对象
|
||||
*
|
||||
* @author :Wind
|
||||
*/
|
||||
public static AlibabaSmsImpl refresh(AlibabaConfig alibabaConfig){
|
||||
*/
|
||||
public static AlibabaSmsImpl refresh(AlibabaConfig alibabaConfig) {
|
||||
// 如果配置对象为空则创建一个
|
||||
if (alibabaSmsConfig == null){
|
||||
if (alibabaSmsConfig == null) {
|
||||
alibabaSmsConfig = new AlibabaSmsConfig();
|
||||
}
|
||||
//重新构造一个实现对象
|
||||
alibabaSms= new AlibabaSmsImpl(alibabaSmsConfig.client(alibabaConfig),
|
||||
alibabaSms = new AlibabaSmsImpl(
|
||||
alibabaConfig,
|
||||
BeanFactory.getExecutor(),
|
||||
BeanFactory.getDelayedTime());
|
||||
|
||||
@ -1,22 +1,18 @@
|
||||
package org.dromara.sms4j.aliyun.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.dtflys.forest.config.ForestConfiguration;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
|
||||
import org.dromara.sms4j.aliyun.utils.AliyunUtils;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.callback.CallBack;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.comm.annotation.Restricted;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.http.HttpJsonTool;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@ -26,6 +22,7 @@ import java.util.concurrent.Executor;
|
||||
/**
|
||||
* <p>类名: AlibabaSmsImpl
|
||||
* <p>说明: 阿里云短信实现
|
||||
*
|
||||
* @author :Wind
|
||||
* 2023/3/26 17:16
|
||||
**/
|
||||
@ -33,22 +30,22 @@ import java.util.concurrent.Executor;
|
||||
@Slf4j
|
||||
public class AlibabaSmsImpl implements SmsBlend {
|
||||
|
||||
private final Client client;
|
||||
|
||||
private final AlibabaConfig alibabaSmsConfig;
|
||||
|
||||
private final Executor pool;
|
||||
|
||||
private final DelayedTime delayed;
|
||||
|
||||
/**
|
||||
* AlibabaSmsImpl
|
||||
* <p>构造器,用于构造短信实现模块
|
||||
* @author :Wind
|
||||
*/
|
||||
private final ForestConfiguration http = BeanFactory.getForestConfiguration();
|
||||
|
||||
public AlibabaSmsImpl(Client client, AlibabaConfig alibabaSmsConfig,Executor pool,DelayedTime delayedTime){
|
||||
this.client = client;
|
||||
/**
|
||||
* AlibabaSmsImpl
|
||||
* <p>构造器,用于构造短信实现模块
|
||||
*
|
||||
* @author :Wind
|
||||
*/
|
||||
|
||||
public AlibabaSmsImpl(AlibabaConfig alibabaSmsConfig, Executor pool, DelayedTime delayedTime) {
|
||||
this.alibabaSmsConfig = alibabaSmsConfig;
|
||||
this.pool = pool;
|
||||
this.delayed = delayedTime;
|
||||
@ -65,34 +62,8 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
@Override
|
||||
@Restricted
|
||||
public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest();
|
||||
String s = JSONObject.toJSONString(messages);
|
||||
sendSmsRequest.setPhoneNumbers(phone)
|
||||
.setTemplateCode(templateId)
|
||||
.setTemplateParam(s)
|
||||
.setSignName(alibabaSmsConfig.getSignature());
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
try {
|
||||
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||
smsResponse.setBizId(sendSmsResponse.body.getBizId());
|
||||
smsResponse.setData(sendSmsResponse.body);
|
||||
smsResponse.setCode(String.valueOf(sendSmsResponse.statusCode));
|
||||
if (!"OK".equals(sendSmsResponse.body.code)) {
|
||||
smsResponse.setErrMessage((sendSmsResponse.body.message));
|
||||
smsResponse.setErrorCode(sendSmsResponse.body.code);
|
||||
} else {
|
||||
smsResponse.setMessage(sendSmsResponse.body.message);
|
||||
}
|
||||
} catch (TeaException error) {
|
||||
log.error(error.getMessage());
|
||||
throw new SmsBlendException(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
log.error(_error.getMessage());
|
||||
throw new SmsBlendException(error.message);
|
||||
}
|
||||
return smsResponse;
|
||||
String messageStr = JSON.toJSONString(messages);
|
||||
return getSmsResponse(phone, messageStr, templateId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -106,32 +77,34 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
@Override
|
||||
@Restricted
|
||||
public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
|
||||
SendBatchSmsRequest sendBatchSmsRequest = new SendBatchSmsRequest();
|
||||
sendBatchSmsRequest.setPhoneNumberJson(JSONObject.toJSONString(phones))//群发的手机号
|
||||
.setTemplateCode(templateId)//模板id
|
||||
.setTemplateParamJson(JSONObject.toJSONString(messages))//消息内容
|
||||
.setSignNameJson(alibabaSmsConfig.getSignature());//短信签名
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
String messageStr = JSON.toJSONString(messages);
|
||||
return getSmsResponse(arrayToString(phones), messageStr, templateId);
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String phone, String message, String templateId) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
String requestUrl;
|
||||
String paramStr;
|
||||
try {
|
||||
SendBatchSmsResponse sendBatchSmsResponse = client.sendBatchSmsWithOptions(sendBatchSmsRequest, runtime);
|
||||
smsResponse.setBizId(sendBatchSmsResponse.body.getBizId());
|
||||
smsResponse.setData(HttpJsonTool.getJSONObject(sendBatchSmsResponse.body));
|
||||
smsResponse.setCode(String.valueOf(sendBatchSmsResponse.statusCode));
|
||||
if (!"OK".equals(sendBatchSmsResponse.body.code)) {
|
||||
smsResponse.setErrMessage((sendBatchSmsResponse.body.message));
|
||||
smsResponse.setErrorCode(sendBatchSmsResponse.body.code);
|
||||
} else {
|
||||
smsResponse.setMessage(sendBatchSmsResponse.body.message);
|
||||
}
|
||||
} catch (TeaException error) {
|
||||
log.error(error.getMessage());
|
||||
throw new SmsBlendException(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
log.error(error.getMessage());
|
||||
throw new SmsBlendException(error.message);
|
||||
requestUrl = AliyunUtils.generateSendSmsRequestUrl(this.alibabaSmsConfig, message, phone, templateId);
|
||||
paramStr = AliyunUtils.generateParamBody(alibabaSmsConfig, phone, message, templateId);
|
||||
} catch (Exception e) {
|
||||
log.error("aliyun send message error", e);
|
||||
throw new SmsBlendException(e.getMessage());
|
||||
}
|
||||
log.info("requestUrl {}", requestUrl);
|
||||
http.post(requestUrl)
|
||||
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
||||
.addBody(paramStr)
|
||||
.onSuccess(((data, req, res) -> {
|
||||
JSONObject jsonBody = res.get(JSONObject.class);
|
||||
log.info(jsonBody.toJSONString());
|
||||
}))
|
||||
.onError((ex, req, res) -> {
|
||||
JSONObject jsonBody = res.get(JSONObject.class);
|
||||
log.info(jsonBody.toJSONString());
|
||||
})
|
||||
.execute();
|
||||
return smsResponse;
|
||||
}
|
||||
|
||||
@ -148,15 +121,15 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
@Restricted
|
||||
public void sendMessageAsync(String phone, String message) {
|
||||
pool.execute(() -> {
|
||||
sendMessage(phone, message);
|
||||
sendMessage(phone, message);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@Restricted
|
||||
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
|
||||
pool.execute(()->{
|
||||
SmsResponse smsResponse = sendMessage(phone,templateId,messages);
|
||||
pool.execute(() -> {
|
||||
SmsResponse smsResponse = sendMessage(phone, templateId, messages);
|
||||
callBack.callBack(smsResponse);
|
||||
});
|
||||
}
|
||||
@ -164,8 +137,8 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
@Override
|
||||
@Restricted
|
||||
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||
pool.execute(()->{
|
||||
sendMessage(phone,templateId,messages);
|
||||
pool.execute(() -> {
|
||||
sendMessage(phone, templateId, messages);
|
||||
});
|
||||
}
|
||||
|
||||
@ -175,9 +148,9 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendMessage(phone,message);
|
||||
sendMessage(phone, message);
|
||||
}
|
||||
},delayedTime);
|
||||
}, delayedTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -186,9 +159,9 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendMessage(phone,templateId,messages);
|
||||
sendMessage(phone, templateId, messages);
|
||||
}
|
||||
},delayedTime);
|
||||
}, delayedTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -197,9 +170,9 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
massTexting(phones,message);
|
||||
massTexting(phones, message);
|
||||
}
|
||||
},delayedTime);
|
||||
}, delayedTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -208,8 +181,16 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
massTexting(phones,templateId,messages);
|
||||
massTexting(phones, templateId, messages);
|
||||
}
|
||||
},delayedTime);
|
||||
}, delayedTime);
|
||||
}
|
||||
|
||||
private String arrayToString(List<String> list) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String s : list) {
|
||||
sb.append(",").append("+86").append(s);
|
||||
}
|
||||
return sb.substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,124 @@
|
||||
package org.dromara.sms4j.aliyun.utils;
|
||||
|
||||
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Richard
|
||||
* @date 2023/4/20 16:55
|
||||
*/
|
||||
public class AliyunUtils {
|
||||
|
||||
/**
|
||||
* 加密方式
|
||||
*/
|
||||
private static final String ALGORITHM = "HMAC-SHA1";
|
||||
|
||||
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
|
||||
public static String generateSendSmsRequestUrl(AlibabaConfig alibabaConfig, String message, String phone, String templateId) throws Exception {
|
||||
// 这里一定要设置GMT时区
|
||||
sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
|
||||
Map<String, String> paras = new HashMap<>();
|
||||
// 1. 公共请求参数
|
||||
paras.put("SignatureMethod", ALGORITHM);
|
||||
paras.put("SignatureNonce", UUID.randomUUID().toString());
|
||||
paras.put("AccessKeyId", alibabaConfig.getAccessKeyId());
|
||||
paras.put("SignatureVersion", "1.0");
|
||||
paras.put("Timestamp", sdf.format(new Date()));
|
||||
paras.put("Format", "JSON");
|
||||
paras.put("Action", alibabaConfig.getAction());
|
||||
paras.put("Version", alibabaConfig.getVersion());
|
||||
paras.put("RegionId", alibabaConfig.getRegionId());
|
||||
// 2. 业务API参数
|
||||
Map<String, String> paramMap = generateParamMap(alibabaConfig, phone, message, templateId);
|
||||
// 3. 参数KEY排序
|
||||
Map<String, String> sortParas = new TreeMap<>(paras);
|
||||
sortParas.putAll(paramMap);
|
||||
// 4. 构造待签名的字符串
|
||||
Iterator<String> it = sortParas.keySet().iterator();
|
||||
StringBuilder sortQueryStringTmp = new StringBuilder();
|
||||
while (it.hasNext()) {
|
||||
String key = it.next();
|
||||
sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(sortParas.get(key)));
|
||||
}
|
||||
|
||||
String stringToSign = "POST" + "&" +
|
||||
specialUrlEncode("/") + "&" +
|
||||
specialUrlEncode(sortQueryStringTmp.substring(1));
|
||||
String signature = sign(alibabaConfig.getAccessKeySecret() + "&", stringToSign);
|
||||
// 5. 生成请求的url参数
|
||||
StringBuilder sortQueryString = new StringBuilder();
|
||||
it = paras.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
String key = it.next();
|
||||
sortQueryString.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(paras.get(key)));
|
||||
}
|
||||
// 6.生成合法请求URL
|
||||
return Constant.HTTPS_PREFIX + alibabaConfig.getRequestUrl() + "/?Signature=" + signature + sortQueryString;
|
||||
}
|
||||
|
||||
/**
|
||||
* url编码
|
||||
*/
|
||||
private static String specialUrlEncode(String value) throws Exception {
|
||||
return URLEncoder.encode(value, StandardCharsets.UTF_8.name()).replace("+", "%20")
|
||||
.replace("*", "%2A").replace("%7E", "~");
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
*
|
||||
* @param accessSecret accessSecret
|
||||
* @param stringToSign 待生成签名的字符串
|
||||
*/
|
||||
private static String sign(String accessSecret, String stringToSign) throws Exception {
|
||||
Mac mac = Mac.getInstance("HmacSHA1");
|
||||
mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));
|
||||
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
|
||||
return new BASE64Encoder().encode(signData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成请求body参数
|
||||
*
|
||||
* @param alibabaConfig 配置数据
|
||||
* @param phone 手机号
|
||||
* @param message 短信内容
|
||||
* @param templateId 模板id
|
||||
*/
|
||||
public static Map<String, String> generateParamMap(AlibabaConfig alibabaConfig, String phone, String message, String templateId) {
|
||||
Map<String, String> paramMap = new HashMap<>();
|
||||
paramMap.put("PhoneNumbers", phone);
|
||||
paramMap.put("SignName", alibabaConfig.getSignature());
|
||||
paramMap.put("TemplateParam", message);
|
||||
paramMap.put("TemplateCode", templateId);
|
||||
return paramMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成请求参数body字符串
|
||||
*
|
||||
* @param alibabaConfig
|
||||
* @param phone
|
||||
* @param message
|
||||
* @param templateId
|
||||
*/
|
||||
public static String generateParamBody(AlibabaConfig alibabaConfig, String phone, String message, String templateId) throws Exception {
|
||||
Map<String, String> paramMap = generateParamMap(alibabaConfig, phone, message, templateId);
|
||||
StringBuilder sortQueryString = new StringBuilder();
|
||||
for (String key : paramMap.keySet()) {
|
||||
sortQueryString.append("&").append(specialUrlEncode(key)).append("=")
|
||||
.append(specialUrlEncode(paramMap.get(key)));
|
||||
}
|
||||
return sortQueryString.substring(1);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,15 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
</parent>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-comm</artifactId>
|
||||
<version>1.0.5</version>
|
||||
<version>2.0.1</version>
|
||||
<name>sms4j-comm</name>
|
||||
<description>sms4j-comm</description>
|
||||
<licenses>
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package org.dromara.sms4j.comm.constant;
|
||||
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Constant
|
||||
* <p> 短信应用常量
|
||||
@ -27,6 +30,8 @@ public abstract class Constant {
|
||||
*/
|
||||
public static final String FROM_URLENCODED = "application/x-www-form-urlencoded";
|
||||
|
||||
public static final String APPLICATION_JSON_UTF8 = "application/json; charset=utf-8";
|
||||
|
||||
/**
|
||||
* 华为云规定 java时间格式
|
||||
*/
|
||||
@ -35,6 +40,10 @@ public abstract class Constant {
|
||||
/** 云片短信国内短信请求地址*/
|
||||
public static final String YUNPIAN_URL = "https://sms.yunpian.com/v2";
|
||||
|
||||
/**
|
||||
* https请求前缀
|
||||
*/
|
||||
public static final String HTTPS_PREFIX = "https://";
|
||||
|
||||
private Constant() {
|
||||
}
|
||||
|
||||
@ -29,4 +29,24 @@ public class TencentConfig extends BaseConfig {
|
||||
*/
|
||||
@Builder.Default
|
||||
private Integer connTimeout = 60;
|
||||
/** 请求地址*/
|
||||
@Builder.Default
|
||||
private String requestUrl = "sms.tencentcloudapi.com";
|
||||
/**
|
||||
* 接口名称
|
||||
*/
|
||||
@Builder.Default
|
||||
private String action = "SendSms";
|
||||
|
||||
/**
|
||||
* 接口版本
|
||||
*/
|
||||
@Builder.Default
|
||||
private String version = "2021-01-11";
|
||||
|
||||
/**
|
||||
* 服务名
|
||||
*/
|
||||
@Builder.Default
|
||||
private String service = "sms";
|
||||
}
|
||||
|
||||
@ -1,15 +1,12 @@
|
||||
package org.dromara.sms4j.tencent.config;
|
||||
|
||||
import com.tencentcloudapi.common.Credential;
|
||||
import com.tencentcloudapi.common.profile.ClientProfile;
|
||||
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||
import com.tencentcloudapi.sms.v20210111.SmsClient;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.tencent.service.TencentSmsImpl;
|
||||
|
||||
/**
|
||||
* TencentSmsConfig
|
||||
* <p> 建造腾讯云短信
|
||||
*
|
||||
* @author :Wind
|
||||
* 2023/4/8 16:05
|
||||
**/
|
||||
@ -21,29 +18,16 @@ public class TencentSmsConfig {
|
||||
|
||||
private static TencentSmsConfig tencentSmsConfig;
|
||||
|
||||
|
||||
|
||||
private SmsClient tencentBean( TencentConfig tencentConfig) {
|
||||
Credential cred = new Credential(tencentConfig.getAccessKeyId(),tencentConfig.getAccessKeySecret());
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setReqMethod("POST");
|
||||
httpProfile.setConnTimeout(tencentConfig.getConnTimeout());
|
||||
httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setSignMethod("HmacSHA256");
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
return new SmsClient(cred, tencentConfig.getTerritory(),clientProfile);
|
||||
}
|
||||
|
||||
/** 建造一个腾讯云的短信实现*/
|
||||
public static TencentSmsImpl createTencentSms(TencentConfig tencentConfig){
|
||||
if (tencentSmsConfig == null){
|
||||
/**
|
||||
* 建造一个腾讯云的短信实现
|
||||
*/
|
||||
public static TencentSmsImpl createTencentSms(TencentConfig tencentConfig) {
|
||||
if (tencentSmsConfig == null) {
|
||||
tencentSmsConfig = new TencentSmsConfig();
|
||||
}
|
||||
if (tencentSms == null){
|
||||
if (tencentSms == null) {
|
||||
tencentSms = new TencentSmsImpl(
|
||||
tencentConfig,
|
||||
tencentSmsConfig.tencentBean(tencentConfig),
|
||||
BeanFactory.getExecutor(),
|
||||
BeanFactory.getDelayedTime()
|
||||
);
|
||||
@ -51,14 +35,15 @@ public class TencentSmsConfig {
|
||||
return tencentSms;
|
||||
}
|
||||
|
||||
/** 刷新对象*/
|
||||
public static TencentSmsImpl refresh(TencentConfig tencentConfig){
|
||||
if (tencentSmsConfig == null){
|
||||
/**
|
||||
* 刷新对象
|
||||
*/
|
||||
public static TencentSmsImpl refresh(TencentConfig tencentConfig) {
|
||||
if (tencentSmsConfig == null) {
|
||||
tencentSmsConfig = new TencentSmsConfig();
|
||||
}
|
||||
tencentSms = new TencentSmsImpl(
|
||||
tencentConfig,
|
||||
tencentSmsConfig.tencentBean(tencentConfig),
|
||||
BeanFactory.getExecutor(),
|
||||
BeanFactory.getDelayedTime()
|
||||
);
|
||||
|
||||
@ -1,36 +1,36 @@
|
||||
package org.dromara.sms4j.tencent.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||||
import com.tencentcloudapi.sms.v20210111.SmsClient;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
||||
import com.dtflys.forest.config.ForestConfiguration;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.callback.CallBack;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.comm.annotation.Restricted;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.tencent.config.TencentConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.tencent.utils.TencentUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@Slf4j
|
||||
public class TencentSmsImpl implements SmsBlend {
|
||||
|
||||
private TencentConfig tencentSmsConfig;
|
||||
|
||||
private SmsClient client;
|
||||
|
||||
private Executor pool;
|
||||
|
||||
private DelayedTime delayed;
|
||||
|
||||
public TencentSmsImpl(TencentConfig tencentSmsConfig, SmsClient client, Executor pool, DelayedTime delayed) {
|
||||
private final ForestConfiguration http = BeanFactory.getForestConfiguration();
|
||||
|
||||
public TencentSmsImpl(TencentConfig tencentSmsConfig, Executor pool, DelayedTime delayed) {
|
||||
this.tencentSmsConfig = tencentSmsConfig;
|
||||
this.client = client;
|
||||
this.pool = pool;
|
||||
this.delayed = delayed;
|
||||
}
|
||||
@ -41,41 +41,20 @@ public class TencentSmsImpl implements SmsBlend {
|
||||
String[] split = message.split("&");
|
||||
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
map.put(String.valueOf(i),split[i]);
|
||||
map.put(String.valueOf(i), split[i]);
|
||||
}
|
||||
return sendMessage(phone, tencentSmsConfig.getTemplateId(),map);
|
||||
return sendMessage(phone, tencentSmsConfig.getTemplateId(), map);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Restricted
|
||||
public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
try {
|
||||
SendSmsRequest req = new SendSmsRequest();
|
||||
req.setSignName(tencentSmsConfig.getSignature());
|
||||
req.setTemplateId(templateId);
|
||||
req.setSmsSdkAppId(tencentSmsConfig.getSdkAppId());
|
||||
List<String> list = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : messages.entrySet()) {
|
||||
list.add(entry.getValue());
|
||||
}
|
||||
String[] s = new String[list.size()];
|
||||
req.setTemplateParamSet(list.toArray(s));
|
||||
req.setPhoneNumberSet(new String[]{"+86" + phone});
|
||||
SendSmsResponse res = client.SendSms(req);
|
||||
String s1 = SendSmsResponse.toJsonString(res);
|
||||
JSONObject jsonObject = JSON.parseObject(s1);
|
||||
if (!"Ok".equals(jsonObject.getString("Code"))) {
|
||||
smsResponse.setErrMessage(jsonObject.getString("Message"));
|
||||
log.debug(smsResponse.getErrMessage());
|
||||
}
|
||||
smsResponse.setMessage(jsonObject.getString("Message"));
|
||||
smsResponse.setBizId(res.getRequestId());
|
||||
smsResponse.setData(jsonObject);
|
||||
} catch (TencentCloudSDKException e) {
|
||||
throw new SmsBlendException(e.getMessage());
|
||||
List<String> list = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : messages.entrySet()) {
|
||||
list.add(entry.getValue());
|
||||
}
|
||||
return smsResponse;
|
||||
String[] s = new String[list.size()];
|
||||
return getSmsResponse(new String[]{"+86" + phone}, list.toArray(s), templateId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -84,40 +63,56 @@ public class TencentSmsImpl implements SmsBlend {
|
||||
String[] split = message.split("&");
|
||||
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
map.put(String.valueOf(i),split[i]);
|
||||
map.put(String.valueOf(i), split[i]);
|
||||
}
|
||||
return massTexting(phones,tencentSmsConfig.getTemplateId(),map);
|
||||
return massTexting(phones, tencentSmsConfig.getTemplateId(), map);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Restricted
|
||||
public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
List<String> list = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : messages.entrySet()) {
|
||||
list.add(entry.getValue());
|
||||
}
|
||||
String[] s = new String[list.size()];
|
||||
return getSmsResponse(arrayToString(phones), list.toArray(s), templateId);
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String[] phones, String[] messages, String templateId) {
|
||||
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
|
||||
String signature;
|
||||
try {
|
||||
SendSmsRequest req = new SendSmsRequest();
|
||||
req.setSignName(tencentSmsConfig.getSignature());
|
||||
req.setTemplateId(templateId);
|
||||
req.setSmsSdkAppId(tencentSmsConfig.getSdkAppId());
|
||||
List<String> list = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : messages.entrySet()) {
|
||||
list.add(entry.getValue());
|
||||
}
|
||||
String[] s = new String[list.size()];
|
||||
req.setTemplateParamSet(list.toArray(s));
|
||||
req.setPhoneNumberSet(arrayToString(phones));
|
||||
SendSmsResponse res = client.SendSms(req);
|
||||
String s1 = SendSmsResponse.toJsonString(res);
|
||||
JSONObject jsonObject = JSON.parseObject(s1);
|
||||
if (!"Ok".equals(jsonObject.getString("Code"))) {
|
||||
smsResponse.setErrMessage(jsonObject.getString("Message"));
|
||||
log.debug(jsonObject.getString("Message"));
|
||||
}
|
||||
smsResponse.setMessage(jsonObject.getString("Message"));
|
||||
smsResponse.setBizId(res.getRequestId());
|
||||
smsResponse.setData(jsonObject);
|
||||
} catch (TencentCloudSDKException e) {
|
||||
signature = TencentUtils.generateSignature(this.tencentSmsConfig, templateId, messages, phones, timestamp);
|
||||
} catch (Exception e) {
|
||||
log.error("tencent send message error", e);
|
||||
throw new SmsBlendException(e.getMessage());
|
||||
}
|
||||
Map<String, Object> headsMap = TencentUtils.generateHeadsMap(signature, timestamp, tencentSmsConfig.getAction(),
|
||||
tencentSmsConfig.getVersion(), tencentSmsConfig.getTerritory(), tencentSmsConfig.getRequestUrl());
|
||||
Map<String, Object> requestBody = TencentUtils.generateRequestBody(phones, tencentSmsConfig.getSdkAppId(),
|
||||
tencentSmsConfig.getSignature(), templateId, messages);
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
String url = Constant.HTTPS_PREFIX + tencentSmsConfig.getRequestUrl();
|
||||
http.post(url)
|
||||
.addHeader(headsMap)
|
||||
.addBody(requestBody)
|
||||
.onSuccess(((data, req, res) -> {
|
||||
JSONObject jsonBody = res.get(JSONObject.class);
|
||||
JSONObject response = jsonBody.getJSONObject("Response");
|
||||
JSONArray sendStatusSet = response.getJSONArray("SendStatusSet");
|
||||
smsResponse.setBizId(sendStatusSet.getJSONObject(0).getString("SerialNo"));
|
||||
smsResponse.setMessage(sendStatusSet.getJSONObject(0).getString("Message"));
|
||||
smsResponse.setCode(sendStatusSet.getJSONObject(0).getString("Code"));
|
||||
}))
|
||||
.onError((ex, req, res) -> {
|
||||
JSONObject jsonBody = res.get(JSONObject.class);
|
||||
JSONObject response = jsonBody.getJSONObject("Response");
|
||||
JSONArray sendStatusSet = response.getJSONArray("SendStatusSet");
|
||||
smsResponse.setErrMessage(sendStatusSet.getJSONObject(0).getString("Message"));
|
||||
smsResponse.setErrorCode(sendStatusSet.getJSONObject(0).getString("Code"));
|
||||
})
|
||||
.execute();
|
||||
return smsResponse;
|
||||
}
|
||||
|
||||
@ -141,8 +136,8 @@ public class TencentSmsImpl implements SmsBlend {
|
||||
@Override
|
||||
@Restricted
|
||||
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
|
||||
pool.execute(()->{
|
||||
SmsResponse smsResponse = sendMessage(phone,templateId,messages);
|
||||
pool.execute(() -> {
|
||||
SmsResponse smsResponse = sendMessage(phone, templateId, messages);
|
||||
callBack.callBack(smsResponse);
|
||||
});
|
||||
}
|
||||
@ -150,8 +145,8 @@ public class TencentSmsImpl implements SmsBlend {
|
||||
@Override
|
||||
@Restricted
|
||||
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||
pool.execute(()->{
|
||||
sendMessage(phone,templateId,messages);
|
||||
pool.execute(() -> {
|
||||
sendMessage(phone, templateId, messages);
|
||||
});
|
||||
}
|
||||
|
||||
@ -161,9 +156,9 @@ public class TencentSmsImpl implements SmsBlend {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendMessage(phone,message);
|
||||
sendMessage(phone, message);
|
||||
}
|
||||
},delayedTime);
|
||||
}, delayedTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -172,9 +167,9 @@ public class TencentSmsImpl implements SmsBlend {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendMessage(phone,templateId,messages);
|
||||
sendMessage(phone, templateId, messages);
|
||||
}
|
||||
},delayedTime);
|
||||
}, delayedTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -183,9 +178,9 @@ public class TencentSmsImpl implements SmsBlend {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
massTexting(phones,message);
|
||||
massTexting(phones, message);
|
||||
}
|
||||
},delayedTime);
|
||||
}, delayedTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -194,16 +189,16 @@ public class TencentSmsImpl implements SmsBlend {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
massTexting(phones,templateId,messages);
|
||||
massTexting(phones, templateId, messages);
|
||||
}
|
||||
},delayedTime);
|
||||
}, delayedTime);
|
||||
}
|
||||
|
||||
private String[] arrayToString(List<String> list){
|
||||
private String[] arrayToString(List<String> list) {
|
||||
String[] strs = new String[list.size()];
|
||||
List<String> toStr = new ArrayList<>();
|
||||
for (String s : list) {
|
||||
toStr.add("+86"+s);
|
||||
toStr.add("+86" + s);
|
||||
}
|
||||
return toStr.toArray(strs);
|
||||
}
|
||||
|
||||
@ -0,0 +1,138 @@
|
||||
package org.dromara.sms4j.tencent.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.tencent.config.TencentConfig;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
|
||||
/**
|
||||
* @author Richard
|
||||
* @date 2023-04-18 19:50
|
||||
*/
|
||||
@Slf4j
|
||||
public class TencentUtils {
|
||||
/**
|
||||
* 加密方式
|
||||
*/
|
||||
private static final String ALGORITHM = "TC3-HMAC-SHA256";
|
||||
/**
|
||||
* 请求方式
|
||||
*/
|
||||
private static final String HTTP_REQUEST_METHOD = "POST";
|
||||
|
||||
private static final String CT_JSON = "application/json; charset=utf-8";
|
||||
|
||||
|
||||
private static byte[] hmac256(byte[] key, String msg) throws Exception {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key, mac.getAlgorithm());
|
||||
mac.init(secretKeySpec);
|
||||
return mac.doFinal(msg.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String sha256Hex(String s) throws Exception {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] d = md.digest(s.getBytes(StandardCharsets.UTF_8));
|
||||
return DatatypeConverter.printHexBinary(d).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成腾讯云发送短信接口签名
|
||||
*
|
||||
* @param templateId 模板id
|
||||
* @param messages 短信内容
|
||||
* @param phones 手机号
|
||||
* @param timestamp 时间戳
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String generateSignature(TencentConfig tencentConfig, String templateId, String[] messages, String[] phones,
|
||||
String timestamp) throws Exception {
|
||||
// ************* 步骤 1:拼接规范请求串 *************
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
String date = sdf.format(new Date(Long.parseLong(timestamp + "000")));
|
||||
String canonicalUri = "/";
|
||||
String canonicalQueryString = "";
|
||||
String canonicalHeaders = "content-type:application/json; charset=utf-8\n" + "host:" + tencentConfig.getRequestUrl() + "\n";
|
||||
String signedHeaders = "content-type;host";
|
||||
HashMap<String, Object> params = new HashMap<>();
|
||||
// 实际调用需要更新参数,这里仅作为演示签名验证通过的例子
|
||||
params.put("PhoneNumberSet", phones);
|
||||
params.put("SmsSdkAppId", tencentConfig.getSdkAppId());
|
||||
params.put("SignName", tencentConfig.getSignature());
|
||||
params.put("TemplateId", templateId);
|
||||
params.put("TemplateParamSet", messages);
|
||||
String payload = JSON.toJSONString(params);
|
||||
String hashedRequestPayload = sha256Hex(payload);
|
||||
String canonicalRequest = HTTP_REQUEST_METHOD + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n"
|
||||
+ canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestPayload;
|
||||
// ************* 步骤 2:拼接待签名字符串 *************
|
||||
String credentialScope = date + "/" + tencentConfig.getService() + "/" + "tc3_request";
|
||||
String hashedCanonicalRequest = sha256Hex(canonicalRequest);
|
||||
String stringToSign = ALGORITHM + "\n" + timestamp + "\n" + credentialScope + "\n" + hashedCanonicalRequest;
|
||||
// ************* 步骤 3:计算签名 *************
|
||||
byte[] secretDate = hmac256(("TC3" + tencentConfig.getAccessKeySecret()).getBytes(StandardCharsets.UTF_8), date);
|
||||
byte[] secretService = hmac256(secretDate, tencentConfig.getService());
|
||||
byte[] secretSigning = hmac256(secretService, "tc3_request");
|
||||
String signature = DatatypeConverter.printHexBinary(hmac256(secretSigning, stringToSign)).toLowerCase();
|
||||
// ************* 步骤 4:拼接 Authorization *************
|
||||
return ALGORITHM + " " + "Credential=" + tencentConfig.getAccessKeyId() + "/" + credentialScope + ", "
|
||||
+ "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成腾讯云短信请求头map
|
||||
*
|
||||
* @param authorization 签名信息
|
||||
* @param timestamp 时间戳
|
||||
* @param action 接口名称
|
||||
* @param version 接口版本
|
||||
* @param territory 服务器地区
|
||||
* @param requestUrl 请求地址
|
||||
*/
|
||||
public static Map<String, Object> generateHeadsMap(String authorization, String timestamp, String action,
|
||||
String version, String territory, String requestUrl) {
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
headers.put("Authorization", authorization);
|
||||
headers.put("Content-Type", CT_JSON);
|
||||
headers.put("Host", requestUrl);
|
||||
headers.put("X-TC-Action", action);
|
||||
headers.put("X-TC-Timestamp", timestamp);
|
||||
headers.put("X-TC-Version", version);
|
||||
headers.put("X-TC-Region", territory);
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成腾讯云短信请求body
|
||||
*
|
||||
* @param phones 手机号
|
||||
* @param sdkAppId appid
|
||||
* @param signatureName 短信签名
|
||||
* @param templateId 模板id
|
||||
* @param templateParamSet 模板参数
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, Object> generateRequestBody(String[] phones, String sdkAppId, String signatureName,
|
||||
String templateId, String[] templateParamSet) {
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("PhoneNumberSet", phones);
|
||||
requestBody.put("SmsSdkAppId", sdkAppId);
|
||||
requestBody.put("SignName", signatureName);
|
||||
requestBody.put("TemplateId", templateId);
|
||||
requestBody.put("TemplateParamSet", templateParamSet);
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user