新增短信验证
This commit is contained in:
parent
2179f4f527
commit
44ea6dffa6
@ -58,6 +58,11 @@
|
||||
<artifactId>zyjblogs-minio-spring-boot-starter</artifactId>
|
||||
<version>${zyjblogs.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.zyjblogs.starter</groupId>
|
||||
<artifactId>zyjblogs-sms-spring-boot-starter</artifactId>
|
||||
<version>${zyjblogs.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<build>
|
||||
|
13
pom.xml
13
pom.xml
@ -56,9 +56,15 @@
|
||||
<easyexcel.version>2.2.11</easyexcel.version>
|
||||
<bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
|
||||
<nacos-client.version>2.0.4</nacos-client.version>
|
||||
|
||||
<!-- 短信sdk-->
|
||||
<!-- 异步 -->
|
||||
<alibabacloud-dysmsapi20170525.version>1.0.1</alibabacloud-dysmsapi20170525.version>
|
||||
<tencentcloud-sdk-java.version>3.1.612</tencentcloud-sdk-java.version>
|
||||
<!-- 同步 -->
|
||||
<dysmsapi20170525.version>2.0.9</dysmsapi20170525.version>
|
||||
<tencentcloud-sdk-java.version>3.1.600</tencentcloud-sdk-java.version>
|
||||
|
||||
|
||||
<!-- xxl-job-core核心版本号 -->
|
||||
<xxl-job-core.version>2.3.0</xxl-job-core.version>
|
||||
<!-- oos -->
|
||||
@ -289,6 +295,11 @@
|
||||
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
|
||||
<version>${tencentcloud-sdk-java.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<version>${dysmsapi20170525.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
@ -25,5 +25,27 @@ spring:
|
||||
password: ${spring.cloud.nacos.config.password}
|
||||
namespace: ${spring.cloud.nacos.config.namespace}
|
||||
group: public
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
gateway:
|
||||
httpclient:
|
||||
response-timeout: 10000
|
||||
connect-timeout: 1000
|
||||
default-filters[0]:
|
||||
name: Retry
|
||||
#根据默认参数,只重试GET请求,修改参数使POST也支持
|
||||
args:
|
||||
methods[0]: GET
|
||||
methods[1]: POST
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
logging:
|
||||
config: classpath:logback-spring.xml
|
||||
config: classpath:logback-spring.xml
|
||||
|
||||
|
||||
|
@ -86,10 +86,9 @@
|
||||
<artifactId>core</artifactId>
|
||||
<version>3.3.3</version>
|
||||
</dependency>
|
||||
<!-- minio -->
|
||||
<dependency>
|
||||
<groupId>cn.zyjblogs.starter</groupId>
|
||||
<artifactId>zyjblogs-minio-spring-boot-starter</artifactId>
|
||||
<artifactId>zyjblogs-sms-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
|
@ -10,7 +10,6 @@ import cn.zyjblogs.server.user.vo.OAuth2AccessTokenVo;
|
||||
import cn.zyjblogs.starter.common.entity.constant.CommonRedisKeyConstant;
|
||||
import cn.zyjblogs.starter.common.entity.response.HttpCode;
|
||||
import cn.zyjblogs.starter.common.exception.AuthRuntimeException;
|
||||
import cn.zyjblogs.starter.minio.service.MinioService;
|
||||
import cn.zyjblogs.starter.redis.utils.RedisTemplateHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@ -30,14 +29,12 @@ import java.util.concurrent.TimeUnit;
|
||||
public class QrCodeSeriveImpl implements QrCodeSerive {
|
||||
private final TokenGranter tokenGranter;
|
||||
private final RedisTemplateHandler<String, QrCode> redisTemplateHandler;
|
||||
private final MinioService minioService;
|
||||
private final ClientDetailsService clientDetails;
|
||||
private final OAuth2RequestFactory oAuth2RequestFactory;
|
||||
|
||||
public QrCodeSeriveImpl(AuthorizationServerEndpointsConfiguration authorizationServerEndpointsConfiguration,
|
||||
ClientDetailsService clientDetails, RedisTemplateHandler redisTemplateHandler, MinioService minioService) {
|
||||
ClientDetailsService clientDetails, RedisTemplateHandler redisTemplateHandler) {
|
||||
this.redisTemplateHandler = redisTemplateHandler;
|
||||
this.minioService = minioService;
|
||||
this.clientDetails = clientDetails;
|
||||
this.oAuth2RequestFactory = new DefaultOAuth2RequestFactory(clientDetails);
|
||||
this.tokenGranter = authorizationServerEndpointsConfiguration.getEndpointsConfigurer().getTokenGranter();
|
||||
|
@ -106,4 +106,5 @@ security:
|
||||
client-id: ${spring.application.name}
|
||||
client-secret: secret
|
||||
resource:
|
||||
id: ${spring.application.name}
|
||||
id: ${spring.application.name}
|
||||
|
||||
|
@ -0,0 +1,35 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"name": "minio",
|
||||
"type": "cn.zyjblogs.starter.minio.config.MinioProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.minio.config.MinioProperties"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"defaultValue": "",
|
||||
"name": "minio.endpoint",
|
||||
"description": "端点",
|
||||
"type": "java.lang.String"
|
||||
},
|
||||
{
|
||||
"defaultValue": "",
|
||||
"name": "minio.accessKey",
|
||||
"description": "账号",
|
||||
"type": "java.lang.String"
|
||||
},
|
||||
{
|
||||
"defaultValue": "",
|
||||
"name": "minio.secretKey",
|
||||
"description": "密码",
|
||||
"type": "java.lang.String"
|
||||
},
|
||||
{
|
||||
"defaultValue": "",
|
||||
"name": "minio.bucketName",
|
||||
"description": "储存桶",
|
||||
"type": "java.lang.String"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"name": "minio",
|
||||
"type": "cn.zyjblogs.starter.minio.config.MinioProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.minio.config.MinioProperties"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"defaultValue": "",
|
||||
"name": "minio.endpoint",
|
||||
"description": "端点",
|
||||
"type": "java.lang.String"
|
||||
},
|
||||
{
|
||||
"defaultValue": "",
|
||||
"name": "minio.accessKey",
|
||||
"description": "账号",
|
||||
"type": "java.lang.String"
|
||||
},
|
||||
{
|
||||
"defaultValue": "",
|
||||
"name": "minio.secretKey",
|
||||
"description": "密码",
|
||||
"type": "java.lang.String"
|
||||
},
|
||||
{
|
||||
"defaultValue": "",
|
||||
"name": "minio.bucketName",
|
||||
"description": "储存桶",
|
||||
"type": "java.lang.String"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
#Generated by Maven
|
||||
#Mon Jul 25 15:29:06 CST 2022
|
||||
groupId=cn.zyjblogs.starter
|
||||
artifactId=zyjblogs-oauth-spring-boot-starter
|
||||
version=1.0-SNAPSHOT
|
@ -0,0 +1,5 @@
|
||||
#Generated by Maven
|
||||
#Mon Jul 25 15:29:06 CST 2022
|
||||
groupId=cn.zyjblogs.starter
|
||||
artifactId=zyjblogs-rabbitmq-spring-boot-starter
|
||||
version=1.0-SNAPSHOT
|
@ -36,12 +36,20 @@
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
|
||||
<version>${alibabacloud-dysmsapi20170525.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java-sms</artifactId>
|
||||
<version>${tencentcloud-sdk-java.version}</version>
|
||||
<!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
|
||||
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
|
||||
<!-- <version>${tencentcloud-sdk-java.version}</version>-->
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.aliyun</groupId>-->
|
||||
<!-- <artifactId>dysmsapi20170525</artifactId>-->
|
||||
<!-- <version>${dysmsapi20170525.version}</version>-->
|
||||
<!-- </dependency>-->
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
@ -0,0 +1,77 @@
|
||||
package cn.zyjblogs.starter.sms.autoconfigure;
|
||||
|
||||
|
||||
import cn.zyjblogs.starter.sms.config.SmsProperties;
|
||||
import cn.zyjblogs.starter.sms.provider.CompositeSmsGranter;
|
||||
import cn.zyjblogs.starter.sms.provider.SmsGranter;
|
||||
import cn.zyjblogs.starter.sms.provider.aliyun.AliSmsHandleGranter;
|
||||
import cn.zyjblogs.starter.sms.provider.tencent.TenSmsHandleGranter;
|
||||
import com.aliyun.auth.credentials.Credential;
|
||||
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
|
||||
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
|
||||
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||
import com.tencentcloudapi.sms.v20190711.SmsClient;
|
||||
import darabonba.core.client.ClientOverrideConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({SmsProperties.class})
|
||||
public class SmsConfiguration {
|
||||
|
||||
|
||||
@Bean
|
||||
public CompositeSmsGranter smsGranter(SmsProperties smsProperties) {
|
||||
List<SmsGranter> list = new ArrayList<>();
|
||||
list.add(new AliSmsHandleGranter(aliSyncClient(smsProperties), smsProperties.getAliyun()));
|
||||
list.add(new TenSmsHandleGranter(tenClient(smsProperties), smsProperties.getTencent()));
|
||||
return new CompositeSmsGranter(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步客户端
|
||||
*
|
||||
* @param smsProperties
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Bean
|
||||
public AsyncClient aliSyncClient(SmsProperties smsProperties) {
|
||||
SmsProperties.AliyunSmsProperties aliyun = smsProperties.getAliyun();
|
||||
if (aliyun == null || Boolean.FALSE.equals(aliyun.getEnable())) {
|
||||
return null;
|
||||
}
|
||||
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
|
||||
.accessKeyId(aliyun.getAccessKey())
|
||||
.accessKeySecret(aliyun.getAccessSecret())
|
||||
//.securityToken("<your-token>") // use STS token
|
||||
.build());
|
||||
return AsyncClient.builder()
|
||||
.region(aliyun.getRegion())
|
||||
.credentialsProvider(provider)
|
||||
//.serviceConfiguration(Configuration.create()) // Service-level configuration
|
||||
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
|
||||
.overrideConfiguration(
|
||||
ClientOverrideConfiguration.create()
|
||||
.setEndpointOverride(aliyun.getEndpoint())
|
||||
//.setReadTimeout(Duration.ofSeconds(30))
|
||||
)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SmsClient tenClient(SmsProperties smsProperties) {
|
||||
SmsProperties.TencentSmsProperties tencent = smsProperties.getTencent();
|
||||
if (tencent == null || Boolean.FALSE.equals(tencent.getEnable())) {
|
||||
return null;
|
||||
}
|
||||
com.tencentcloudapi.common.Credential credential = new com.tencentcloudapi.common.Credential(tencent.getAccessKey(), tencent.getAccessSecret());
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setEndpoint(tencent.getEndpoint());
|
||||
return new SmsClient(credential, tencent.getRegion());
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package cn.zyjblogs.starter.sms.common;
|
||||
|
||||
public class SmsActionConstant {
|
||||
public static final String SIMPLE_CLIENT_SEND = "simpleSendSms";
|
||||
public static final String ASYNC_CLIENT_SEND = "asyncSendSms";
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package cn.zyjblogs.starter.sms.common;
|
||||
|
||||
public class SmsCommon {
|
||||
/**
|
||||
* 短信 参数key
|
||||
*/
|
||||
public static final String TEMPLATE_PARAM = "templateParam";
|
||||
/**
|
||||
* 腾讯sdk专用 senderid 港澳台专用
|
||||
*/
|
||||
public static final String SENDERID = "senderid";
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package cn.zyjblogs.starter.sms.common;
|
||||
|
||||
public enum SmsEnum {
|
||||
ALI_SMS("aliyun"),
|
||||
TEN_SMS("tencent");
|
||||
private String name;
|
||||
|
||||
SmsEnum(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static SmsEnum findByName(String name) {
|
||||
SmsEnum[] values = SmsEnum.values();
|
||||
for (SmsEnum value : values) {
|
||||
if (value.getName().equals(name)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package cn.zyjblogs.starter.sms.common;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class SmsRequest implements Serializable {
|
||||
/**
|
||||
* 签名
|
||||
*/
|
||||
private String signName;
|
||||
/**
|
||||
* 模板编号
|
||||
*/
|
||||
private String templateCode;
|
||||
/**
|
||||
* 参数map
|
||||
*/
|
||||
private Map<String, Object> paramsMap;
|
||||
/**
|
||||
* 方法
|
||||
*/
|
||||
private String action;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package cn.zyjblogs.starter.sms.common;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class SmsResponse implements Serializable {
|
||||
private String message;
|
||||
private String code;
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package cn.zyjblogs.starter.sms.config;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@ConfigurationProperties(prefix = "sms")
|
||||
public class SmsProperties {
|
||||
|
||||
private AliyunSmsProperties aliyun;
|
||||
private TencentSmsProperties tencent;
|
||||
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class AliyunSmsProperties implements Serializable {
|
||||
private Boolean enable;
|
||||
private String accessKey;
|
||||
//秘钥
|
||||
private String accessSecret;
|
||||
//ip地址
|
||||
private String endpoint;
|
||||
//地区
|
||||
private String region;
|
||||
//模板编号
|
||||
private String templateCode;
|
||||
/**
|
||||
* 批量发送最数量
|
||||
*/
|
||||
private Integer maxSize;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class TencentSmsProperties implements Serializable {
|
||||
private Boolean enable;
|
||||
private String accessKey;
|
||||
//秘钥
|
||||
private String accessSecret;
|
||||
//ip地址
|
||||
private String endpoint;
|
||||
//地区
|
||||
private String region;
|
||||
//模板编号
|
||||
private String templateCode;
|
||||
private String sign;
|
||||
private String appId;
|
||||
private String senderId;
|
||||
/**
|
||||
* 批量发送最数量
|
||||
*/
|
||||
private Integer maxSize;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package cn.zyjblogs.starter.sms.exception;
|
||||
|
||||
public class SmsRuntimeException extends RuntimeException {
|
||||
|
||||
public SmsRuntimeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package cn.zyjblogs.starter.sms.provider;
|
||||
|
||||
import cn.zyjblogs.starter.sms.common.SmsRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractSmsGranter implements SmsGranter {
|
||||
private final String typeName;
|
||||
|
||||
public AbstractSmsGranter(String typeName) {
|
||||
this.typeName = typeName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsGranter getGranter(String typeName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSms(String typeName, List<String> phones, SmsRequest smsRequest) {
|
||||
sendSms(phones, smsRequest);
|
||||
}
|
||||
|
||||
public void sendSms(List<String> phones, SmsRequest smsRequest) {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package cn.zyjblogs.starter.sms.provider;
|
||||
|
||||
import cn.zyjblogs.starter.sms.common.SmsRequest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CompositeSmsGranter implements SmsGranter {
|
||||
private final List<SmsGranter> smsGranters;
|
||||
|
||||
public CompositeSmsGranter(List<SmsGranter> smsGranters) {
|
||||
this.smsGranters = new ArrayList<>(smsGranters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsGranter getGranter(String typeName) {
|
||||
for (SmsGranter granter : smsGranters) {
|
||||
SmsGranter granters = granter.getGranter(typeName);
|
||||
if (granters != null) {
|
||||
return granters;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSms(String typeName, List<String> phones, SmsRequest smsRequest) {
|
||||
SmsGranter granter1 = getGranter(typeName);
|
||||
if (granter1 != null) {
|
||||
granter1.sendSms(typeName, phones, smsRequest);
|
||||
}
|
||||
}
|
||||
|
||||
public void addTokenGranter(SmsGranter smsGranter) {
|
||||
if (smsGranter == null) {
|
||||
throw new IllegalArgumentException("sms granter is null");
|
||||
}
|
||||
smsGranters.add(smsGranter);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package cn.zyjblogs.starter.sms.provider;
|
||||
|
||||
import cn.zyjblogs.starter.sms.common.SmsRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SmsGranter {
|
||||
SmsGranter getGranter(String typeName);
|
||||
|
||||
/**
|
||||
* 短信发送 ,手机号为多个以逗号隔开
|
||||
*
|
||||
* @param typeName
|
||||
* @param phones
|
||||
* @param smsRequest
|
||||
* @return
|
||||
*/
|
||||
void sendSms(String typeName, List<String> phones, SmsRequest smsRequest);
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package cn.zyjblogs.starter.sms.provider.aliyun;
|
||||
|
||||
import cn.zyjblogs.starter.sms.common.SmsCommon;
|
||||
import cn.zyjblogs.starter.sms.common.SmsEnum;
|
||||
import cn.zyjblogs.starter.sms.common.SmsRequest;
|
||||
import cn.zyjblogs.starter.sms.config.SmsProperties;
|
||||
import cn.zyjblogs.starter.sms.exception.SmsRuntimeException;
|
||||
import cn.zyjblogs.starter.sms.provider.AbstractSmsGranter;
|
||||
import cn.zyjblogs.starter.sms.provider.SmsGranter;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
|
||||
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
|
||||
import wiremock.com.google.common.collect.Lists;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AliSmsHandleGranter extends AbstractSmsGranter {
|
||||
private static final String TYPE_NAME = SmsEnum.ALI_SMS.getName();
|
||||
private final AsyncClient aliSyncClient;
|
||||
private final SmsProperties.AliyunSmsProperties aliyunSmsProperties;
|
||||
|
||||
public AliSmsHandleGranter(AsyncClient aliSyncClient, SmsProperties.AliyunSmsProperties aliyunSmsProperties, String typeName) {
|
||||
super(typeName);
|
||||
this.aliSyncClient = aliSyncClient;
|
||||
this.aliyunSmsProperties = aliyunSmsProperties;
|
||||
if (aliyunSmsProperties.getMaxSize() == null || aliyunSmsProperties.getMaxSize() < 100) {
|
||||
aliyunSmsProperties.setMaxSize(200);
|
||||
}
|
||||
}
|
||||
|
||||
public AliSmsHandleGranter(AsyncClient aliSyncClient, SmsProperties.AliyunSmsProperties aliyunSmsProperties) {
|
||||
super(TYPE_NAME);
|
||||
this.aliSyncClient = aliSyncClient;
|
||||
this.aliyunSmsProperties = aliyunSmsProperties;
|
||||
if (aliyunSmsProperties.getMaxSize() == null || aliyunSmsProperties.getMaxSize() < 100) {
|
||||
aliyunSmsProperties.setMaxSize(200);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信发送 ,手机号为多个以逗号隔开
|
||||
*
|
||||
* @param phones
|
||||
* @param smsRequest
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void sendSms(List<String> phones, SmsRequest smsRequest) {
|
||||
Object o = smsRequest.getParamsMap().get(SmsCommon.TEMPLATE_PARAM);
|
||||
if (o == null) {
|
||||
throw new SmsRuntimeException("参数不能为空");
|
||||
}
|
||||
try {
|
||||
String templateCode = smsRequest.getTemplateCode();
|
||||
String templateParam = JSON.toJSONString(o);
|
||||
List<List<String>> partition = Lists.partition(phones, aliyunSmsProperties.getMaxSize());
|
||||
partition.forEach(phoneList -> {
|
||||
SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
|
||||
.phoneNumbers(String.join(",", phoneList))
|
||||
.signName(smsRequest.getSignName())
|
||||
.templateCode(templateCode)
|
||||
.templateParam(templateParam)
|
||||
.build();
|
||||
aliSyncClient.sendSms(sendSmsRequest);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
throw new SmsRuntimeException("发送失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsGranter getGranter(String typeName) {
|
||||
if (TYPE_NAME.equals(typeName)) {
|
||||
return this;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package cn.zyjblogs.starter.sms.provider.tencent;
|
||||
|
||||
import cn.zyjblogs.starter.sms.common.SmsCommon;
|
||||
import cn.zyjblogs.starter.sms.common.SmsEnum;
|
||||
import cn.zyjblogs.starter.sms.common.SmsRequest;
|
||||
import cn.zyjblogs.starter.sms.config.SmsProperties;
|
||||
import cn.zyjblogs.starter.sms.exception.SmsRuntimeException;
|
||||
import cn.zyjblogs.starter.sms.provider.AbstractSmsGranter;
|
||||
import cn.zyjblogs.starter.sms.provider.SmsGranter;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||||
import com.tencentcloudapi.sms.v20190711.SmsClient;
|
||||
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TenSmsHandleGranter extends AbstractSmsGranter {
|
||||
private static final String TYPE_NAME = SmsEnum.TEN_SMS.getName();
|
||||
private final SmsClient smsClient;
|
||||
private final SmsProperties.TencentSmsProperties tencentSmsProperties;
|
||||
|
||||
public TenSmsHandleGranter(SmsClient smsClient, SmsProperties.TencentSmsProperties tencentSmsProperties, String typeName) {
|
||||
super(typeName);
|
||||
this.smsClient = smsClient;
|
||||
this.tencentSmsProperties = tencentSmsProperties;
|
||||
if (tencentSmsProperties.getMaxSize() == null || tencentSmsProperties.getMaxSize() < 100) {
|
||||
tencentSmsProperties.setMaxSize(200);
|
||||
}
|
||||
}
|
||||
|
||||
public TenSmsHandleGranter(SmsClient smsClient, SmsProperties.TencentSmsProperties tencentSmsProperties) {
|
||||
super(TYPE_NAME);
|
||||
this.smsClient = smsClient;
|
||||
this.tencentSmsProperties = tencentSmsProperties;
|
||||
if (tencentSmsProperties.getMaxSize() == null || tencentSmsProperties.getMaxSize() < 100) {
|
||||
tencentSmsProperties.setMaxSize(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSms(List<String> phones, SmsRequest smsRequest) {
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest();
|
||||
sendSmsRequest.setPhoneNumberSet(phones.toArray(new String[0]));
|
||||
sendSmsRequest.setSmsSdkAppid(tencentSmsProperties.getAppId());
|
||||
sendSmsRequest.setSign(tencentSmsProperties.getSign());
|
||||
if (!StringUtils.hasLength(smsRequest.getTemplateCode())) {
|
||||
throw new SmsRuntimeException("模板id不能为空");
|
||||
}
|
||||
Map<String, Object> paramsMap = smsRequest.getParamsMap();
|
||||
Object template = paramsMap.get(SmsCommon.TEMPLATE_PARAM);
|
||||
if (template == null) {
|
||||
throw new SmsRuntimeException("参数不能为空");
|
||||
}
|
||||
List<String> lists = JSON.parseArray(JSON.toJSONString(template), String.class);
|
||||
sendSmsRequest.setTemplateParamSet(lists.toArray(new String[0]));
|
||||
if (paramsMap.containsKey(SmsCommon.SENDERID)) {
|
||||
sendSmsRequest.setSenderId(paramsMap.get(SmsCommon.SENDERID).toString());
|
||||
}
|
||||
try {
|
||||
smsClient.SendSms(sendSmsRequest);
|
||||
} catch (TencentCloudSDKException e) {
|
||||
throw new SmsRuntimeException("发送失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsGranter getGranter(String typeName) {
|
||||
if (TYPE_NAME.equals(typeName)) {
|
||||
return this;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"name": "sms",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"name": "sms",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.enable",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.accessKey",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.accessSecret",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.endpoint",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.region",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.templateCode",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.maxSize",
|
||||
"type": "java.lang.Integer",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": 200
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.enable",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.accessKey",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.accessSecret",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.endpoint",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.region",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.templateCode",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.sign",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.appId",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.senderId",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.maxSize",
|
||||
"type": "java.lang.Integer",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": 200
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
#Generated by Maven
|
||||
#Mon Jul 25 15:29:06 CST 2022
|
||||
groupId=cn.zyjblogs.starter
|
||||
artifactId=zyjblogs-sms-spring-boot-starter
|
||||
version=1.0-SNAPSHOT
|
@ -36,8 +36,20 @@
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
|
||||
<version>${alibabacloud-dysmsapi20170525.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java-sms</artifactId>
|
||||
<!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
|
||||
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
|
||||
<!-- <version>${tencentcloud-sdk-java.version}</version>-->
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.aliyun</groupId>-->
|
||||
<!-- <artifactId>dysmsapi20170525</artifactId>-->
|
||||
<!-- <version>${dysmsapi20170525.version}</version>-->
|
||||
<!-- </dependency>-->
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
@ -0,0 +1,138 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"name": "sms",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"name": "sms",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.enable",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.accessKey",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.accessSecret",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.endpoint",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.region",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.templateCode",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.aliyun.maxSize",
|
||||
"type": "java.lang.Integer",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$AliyunSmsProperties",
|
||||
"defaultValue": 200
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent",
|
||||
"type": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties"
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.enable",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.accessKey",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.accessSecret",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.endpoint",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.region",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.templateCode",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.sign",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.appId",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.senderId",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": ""
|
||||
},
|
||||
{
|
||||
"name": "sms.tencent.maxSize",
|
||||
"type": "java.lang.Integer",
|
||||
"sourceType": "cn.zyjblogs.starter.sms.config.SmsProperties$TencentSmsProperties",
|
||||
"defaultValue": 200
|
||||
}
|
||||
]
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
#org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
# cn.zyjblogs.starter.minio.autoconfigure.MinioAutoConfiguration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.zyjblogs.starter.sms.autoconfigure.SmsConfiguration
|
@ -0,0 +1,5 @@
|
||||
#Generated by Maven
|
||||
#Mon Jul 25 15:29:06 CST 2022
|
||||
groupId=cn.zyjblogs.starter
|
||||
artifactId=zyjblogs-web-spring-boot-starter
|
||||
version=1.0-SNAPSHOT
|
Loading…
Reference in New Issue
Block a user