mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
!31 fix 修复阿里云短信签名异常、打印日志为Null问题,并优化javax加密包
* fix 修复阿里云短信签名异常、打印日志为Null问题,并优化javax加密包
This commit is contained in:
parent
d9b5b12a69
commit
a4c6bf64d6
@ -1,7 +1,6 @@
|
||||
package org.dromara.sms4j.aliyun.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dtflys.forest.config.ForestConfiguration;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
|
||||
@ -16,9 +15,11 @@ import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* <p>类名: AlibabaSmsImpl
|
||||
@ -83,7 +84,7 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String phone, String message, String templateId) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
AtomicReference<SmsResponse> reference = new AtomicReference<>();
|
||||
String requestUrl;
|
||||
String paramStr;
|
||||
try {
|
||||
@ -98,14 +99,24 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
||||
.addBody(paramStr)
|
||||
.onSuccess(((data, req, res) -> {
|
||||
JSONObject jsonBody = res.get(JSONObject.class);
|
||||
log.info(jsonBody.toJSONString());
|
||||
Map map = res.get(Map.class);
|
||||
reference.set(getResponse(map));
|
||||
}))
|
||||
.onError((ex, req, res) -> {
|
||||
JSONObject jsonBody = res.get(JSONObject.class);
|
||||
log.info(jsonBody.toJSONString());
|
||||
Map map = res.get(Map.class);
|
||||
reference.set(getResponse(map));
|
||||
})
|
||||
.execute();
|
||||
return reference.get();
|
||||
}
|
||||
|
||||
private static SmsResponse getResponse(Map map) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
smsResponse.setCode((String) map.get("Code"));
|
||||
smsResponse.setMessage((String) map.get("Message"));
|
||||
if ("OK".equals(smsResponse.getCode())){
|
||||
smsResponse.setBizId((String) map.get("BizId"));
|
||||
}
|
||||
return smsResponse;
|
||||
}
|
||||
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
package org.dromara.sms4j.aliyun.utils;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.crypto.digest.HMac;
|
||||
import cn.hutool.crypto.digest.HmacAlgorithm;
|
||||
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -21,18 +21,18 @@ public class AliyunUtils {
|
||||
*/
|
||||
private static final String ALGORITHM = "HMAC-SHA1";
|
||||
|
||||
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
private static final 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"));
|
||||
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("Timestamp", SDF.format(new Date()));
|
||||
paras.put("Format", "JSON");
|
||||
paras.put("Action", alibabaConfig.getAction());
|
||||
paras.put("Version", alibabaConfig.getVersion());
|
||||
@ -80,10 +80,8 @@ public class AliyunUtils {
|
||||
* @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 Base64.encode(signData);
|
||||
HMac hMac = new HMac(HmacAlgorithm.HmacSHA1, accessSecret.getBytes());
|
||||
return hMac.digestBase64(stringToSign,StandardCharsets.UTF_8, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user