perf:调整阿里云短信,移除官方sdk
This commit is contained in:
11
pom.xml
11
pom.xml
@@ -114,17 +114,6 @@
|
||||
<version>3.1.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-openapi</artifactId>
|
||||
<version>0.2.8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<version>2.0.23</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
|
||||
@@ -23,6 +23,12 @@
|
||||
<groupId>com.tiesheng</groupId>
|
||||
<artifactId>tiesheng-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.tiesheng.demo.controller;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.tiesheng.annotation.token.TokenIgnore;
|
||||
import com.tiesheng.message.config.aliyun.AliyunSmsConfig;
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
import com.tiesheng.util.config.GlobalConfig;
|
||||
import com.tiesheng.util.pojos.ApiResp;
|
||||
@@ -19,8 +21,8 @@ public class TestController {
|
||||
|
||||
@Autowired
|
||||
GlobalConfig globalConfig;
|
||||
// @Autowired
|
||||
// AliyunSmsConfig aliyunSmsConfig;
|
||||
@Autowired
|
||||
AliyunSmsConfig aliyunSmsConfig;
|
||||
|
||||
@RequestMapping("/index")
|
||||
@TokenIgnore
|
||||
@@ -38,9 +40,9 @@ public class TestController {
|
||||
@RequestMapping("/send")
|
||||
@TokenIgnore
|
||||
public ApiResp<MessageReqResp> sendMessage() {
|
||||
// MessageReqResp reqResp = aliyunSmsConfig.sendSms("13567116463", "SMS_126361543",
|
||||
// JSONUtil.createObj().putOpt("code", "123456"));
|
||||
return ApiResp.respOK(new MessageReqResp());
|
||||
MessageReqResp reqResp = aliyunSmsConfig.sendSms("13567116463", "SMS_154950909",
|
||||
JSONUtil.createObj().putOpt("code", "123456"));
|
||||
return ApiResp.respOK(reqResp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,4 +25,4 @@ tiesheng:
|
||||
aliyun:
|
||||
access-key-id: LTAI5tJtbgBCnTY5eS4SmrTf
|
||||
access-key-secret: JIHqpRUFffCHhXaJEVvWN31WcexWqG
|
||||
sign-name: 智慧校园
|
||||
sign-name: 阿里云短信测试
|
||||
|
||||
@@ -24,18 +24,6 @@
|
||||
<artifactId>tiesheng-platform</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--阿里云短信登录-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-openapi</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,28 +1,34 @@
|
||||
package com.tiesheng.message.config.aliyun;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
import com.tiesheng.message.service.TieshengMessageConfigurer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "tiesheng.aliyun")
|
||||
@ConditionalOnClass(SendSmsResponse.class)
|
||||
public class AliyunSmsConfig {
|
||||
|
||||
/**
|
||||
* 请求地址
|
||||
*/
|
||||
private static final String ENDPOINT = "https://dysmsapi.aliyuncs.com";
|
||||
|
||||
@Autowired
|
||||
TieshengMessageConfigurer tieshengMessageConfigurer;
|
||||
|
||||
@@ -31,47 +37,82 @@ public class AliyunSmsConfig {
|
||||
private String signName;
|
||||
|
||||
/**
|
||||
* 获取一个短信发送客户端
|
||||
* 特殊字符替换
|
||||
*
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
private Client getClient() throws Exception {
|
||||
|
||||
Config config = new Config()
|
||||
.setAccessKeyId(getAccessKeyId())
|
||||
.setAccessKeySecret(getAccessKeySecret());
|
||||
config.setEndpoint("dysmsapi.aliyuncs.com");
|
||||
return new Client(config);
|
||||
private static String specialUrlEncode(String value) {
|
||||
try {
|
||||
return URLEncoder.encode(value, "UTF-8").replace("+", "%20")
|
||||
.replace("*", "%2A").replace("%7E", "~");
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
* 创建发送请求
|
||||
*
|
||||
* @return 异常说明,为空是表示正常
|
||||
* @param phoneNumbers 手机号,多个用,隔开
|
||||
* @param templateCode 短信模板
|
||||
* @param params 模板参数
|
||||
* @return
|
||||
*/
|
||||
public MessageReqResp sendSms(String phones, String templateCode, JSONObject params) {
|
||||
public MessageReqResp sendSms(String phoneNumbers, String templateCode, JSONObject params) {
|
||||
|
||||
MessageReqResp reqResp = new MessageReqResp();
|
||||
reqResp.setTarget(phones);
|
||||
reqResp.setResult(0);
|
||||
reqResp.setTarget(phoneNumbers);
|
||||
reqResp.setResult(1);
|
||||
reqResp.setContent(JSONUtil.createObj()
|
||||
.putOpt("PhoneNumbers", phoneNumbers)
|
||||
.putOpt("SignName", getSignName())
|
||||
.putOpt("TemplateCode", templateCode)
|
||||
.putOpt("TemplateParam", params)
|
||||
.toString());
|
||||
|
||||
SendSmsRequest request = new SendSmsRequest();
|
||||
request.setPhoneNumbers(phones);
|
||||
request.setSignName(getSignName());
|
||||
request.setTemplateCode(templateCode);
|
||||
SortedMap<String, String> queryMap = new TreeMap<>();
|
||||
|
||||
// 系统参数
|
||||
queryMap.put("AccessKeyId", getAccessKeyId());
|
||||
queryMap.put("SignatureMethod", "HMAC-SHA1");
|
||||
queryMap.put("SignatureNonce", IdUtil.randomUUID());
|
||||
queryMap.put("SignatureVersion", "1.0");
|
||||
SimpleDateFormat dateFormat = DateUtil.newSimpleFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.CHINESE, TimeZone.getTimeZone("0"));
|
||||
queryMap.put("Timestamp", DateUtil.format(DateUtil.date(), dateFormat));
|
||||
queryMap.put("Format", "json");
|
||||
|
||||
// 业务API参数
|
||||
queryMap.put("Action", "SendSms");
|
||||
queryMap.put("Version", "2017-05-25");
|
||||
queryMap.put("RegionId", "cn-hangzhou");
|
||||
queryMap.put("PhoneNumbers", phoneNumbers);
|
||||
queryMap.put("SignName", getSignName());
|
||||
queryMap.put("TemplateCode", templateCode);
|
||||
if (params != null) {
|
||||
request.setTemplateParam(params.toString());
|
||||
queryMap.put("TemplateParam", params.toString());
|
||||
}
|
||||
|
||||
reqResp.setContent(JSONUtil.toJsonStr(request));
|
||||
try {
|
||||
SendSmsResponse response = getClient().sendSms(request);
|
||||
reqResp.setRespBody(JSONUtil.toJsonStr(response.body));
|
||||
if (Objects.equals(response.getBody().getCode(), "OK")) {
|
||||
reqResp.setResult(1);
|
||||
} else {
|
||||
reqResp.setToast(response.getBody().getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
reqResp.setToast("未知异常,请稍后再试");
|
||||
// 构造带签名字符串
|
||||
StringBuilder sortQueryStringTmp = new StringBuilder();
|
||||
for (String key : queryMap.keySet()) {
|
||||
sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(queryMap.get(key)));
|
||||
}
|
||||
String sortedQueryString = sortQueryStringTmp.substring(1);
|
||||
String stringToSign = "GET" + "&" + specialUrlEncode("/") + "&" + specialUrlEncode(sortedQueryString);
|
||||
String digest = SecureUtil.hmacSha1((getAccessKeySecret() + "&").getBytes(StandardCharsets.UTF_8))
|
||||
.digestBase64(stringToSign, false);
|
||||
String signature = specialUrlEncode(digest);
|
||||
queryMap.put("Signature", signature);
|
||||
|
||||
// 发送请求
|
||||
String response = HttpUtil.get(ENDPOINT + "?Signature=" + signature + sortQueryStringTmp, 10 * 1000);
|
||||
reqResp.setRespBody(response);
|
||||
JSONObject respObj = JSONUtil.parseObj(response);
|
||||
if (!Objects.equals(respObj.getStr("Code"), "OK")) {
|
||||
reqResp.setResult(0);
|
||||
reqResp.setToast(respObj.getStr("Message"));
|
||||
}
|
||||
|
||||
tieshengMessageConfigurer.onMessageSend("阿里云短信", reqResp);
|
||||
@@ -79,7 +120,6 @@ public class AliyunSmsConfig {
|
||||
return reqResp;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user