Skip to content

Commit cc04512

Browse files
committed
添加集成阿里云国际短信服务.
1 parent a13ccc3 commit cc04512

File tree

16 files changed

+447
-5
lines changed

16 files changed

+447
-5
lines changed

api-boot-project/api-boot-autoconfigure/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,12 @@
8585
<optional>true</optional>
8686
</dependency>
8787

88+
<!--ApiBoot Sms Plugin-->
89+
<dependency>
90+
<groupId>org.minbox.framework</groupId>
91+
<artifactId>api-boot-plugin-alibaba-sms</artifactId>
92+
<optional>true</optional>
93+
</dependency>
94+
8895
</dependencies>
8996
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.minbox.framework.api.boot.autoconfigure.sms;
2+
3+
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
4+
import org.minbox.framework.api.boot.plugin.sms.ApiBootAliYunSmsService;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
import static org.minbox.framework.api.boot.autoconfigure.sms.ApiBootSmsProperties.API_BOOT_SMS_PREFIX;
13+
14+
/**
15+
* ApiBoot 短信服务自动化配置
16+
*
17+
* @author:恒宇少年 - 于起宇
18+
* <p>
19+
* DateTime:2019-03-22 10:53
20+
* Blog:http://blog.yuqiyu.com
21+
* WebSite:http://www.jianshu.com/u/092df3f77bca
22+
* Gitee:https://gitee.com/hengboy
23+
* GitHub:https://github.com/hengyuboy
24+
*/
25+
@Configuration
26+
@ConditionalOnClass(SendSmsRequest.class)
27+
@ConditionalOnProperty(prefix = API_BOOT_SMS_PREFIX, name = {"access-key-id", "access-key-secret", "sign-name"})
28+
@EnableConfigurationProperties(ApiBootSmsProperties.class)
29+
public class ApiBootSmsAutoConfiguration {
30+
/**
31+
* ApiBoot Sms 属性配置类
32+
*/
33+
private ApiBootSmsProperties apiBootSmsProperties;
34+
35+
public ApiBootSmsAutoConfiguration(ApiBootSmsProperties apiBootSmsProperties) {
36+
this.apiBootSmsProperties = apiBootSmsProperties;
37+
}
38+
39+
/**
40+
* 实例化ApiBoot Sms Service
41+
*
42+
* @return ApiBoot Sms Service
43+
*/
44+
@Bean
45+
@ConditionalOnMissingBean
46+
ApiBootAliYunSmsService apiBootSmsService() {
47+
return new ApiBootAliYunSmsService(apiBootSmsProperties.getAccessKeyId(), apiBootSmsProperties.getAccessKeySecret(), apiBootSmsProperties.getSignName(), apiBootSmsProperties.getProfile(), apiBootSmsProperties.getConnectionTimeout(), apiBootSmsProperties.getReadTimeout());
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.minbox.framework.api.boot.autoconfigure.sms;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
import static org.minbox.framework.api.boot.autoconfigure.sms.ApiBootSmsProperties.API_BOOT_SMS_PREFIX;
8+
9+
/**
10+
* ApiBoot 集成 阿里云国际短信服务配置类
11+
*
12+
* @author:恒宇少年 - 于起宇
13+
* <p>
14+
* DateTime:2019-03-22 10:53
15+
* Blog:http://blog.yuqiyu.com
16+
* WebSite:http://www.jianshu.com/u/092df3f77bca
17+
* Gitee:https://gitee.com/hengboy
18+
* GitHub:https://github.com/hengyuboy
19+
*/
20+
@Data
21+
@Configuration
22+
@ConfigurationProperties(prefix = API_BOOT_SMS_PREFIX)
23+
public class ApiBootSmsProperties {
24+
/**
25+
* sms 配置前缀
26+
*/
27+
public static final String API_BOOT_SMS_PREFIX = "api.boot.sms";
28+
/**
29+
* RAM账号的AccessKey ID
30+
*/
31+
private String accessKeyId;
32+
/**
33+
* RAM账号Access Key Secret
34+
*/
35+
private String accessKeySecret;
36+
/**
37+
* 短信签名
38+
*/
39+
private String signName;
40+
/**
41+
* 短信发送连接超时时长
42+
*/
43+
private long connectionTimeout = 10000;
44+
/**
45+
* 短信接收消息连接超时时长
46+
*/
47+
private long readTimeout = 10000;
48+
/**
49+
* 短信环境
50+
*/
51+
private String profile = "default";
52+
}

api-boot-project/api-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
66
org.minbox.framework.api.boot.autoconfigure.security.authorization.ApiBootAuthorizationMemoryServerAutoConfiguration,\
77
org.minbox.framework.api.boot.autoconfigure.security.authorization.ApiBootAuthorizationServerJdbcAutoConfiguration,\
88
org.minbox.framework.api.boot.autoconfigure.converter.HttpMessageConverterAutoConfiguration,\
9-
org.minbox.framework.api.boot.autoconfigure.oss.ApiBootOssAutoConfiguration
9+
org.minbox.framework.api.boot.autoconfigure.oss.ApiBootOssAutoConfiguration,\
10+
org.minbox.framework.api.boot.autoconfigure.sms.ApiBootSmsAutoConfiguration

api-boot-project/api-boot-dependencies/pom.xml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
<guava.version>20.0</guava.version>
2626
<swagger.version>2.9.2</swagger.version>
2727
<alibaba.oss.version>2.8.3</alibaba.oss.version>
28+
<aliyun.sdk.core.version>4.2.0</aliyun.sdk.core.version>
29+
<aliyun.sdk.dysmsapi.version>1.1.0</aliyun.sdk.dysmsapi.version>
2830
</properties>
2931
<dependencyManagement>
3032
<dependencies>
@@ -73,7 +75,12 @@
7375
<artifactId>api-boot-starter-alibaba-oss</artifactId>
7476
<version>${project.version}</version>
7577
</dependency>
76-
78+
<!--ApiBoot Alibaba Sms-->
79+
<dependency>
80+
<groupId>org.minbox.framework</groupId>
81+
<artifactId>api-boot-starter-alibaba-sms</artifactId>
82+
<version>${project.version}</version>
83+
</dependency>
7784

7885
<!--ApiBoot Common-->
7986
<dependency>
@@ -147,7 +154,14 @@
147154
<version>${swagger.version}</version>
148155
</dependency>
149156

150-
<!--alibaba oss-->
157+
<!--ApiBoot Plugin-->
158+
<dependency>
159+
<groupId>org.minbox.framework</groupId>
160+
<artifactId>api-boot-plugin</artifactId>
161+
<version>${project.version}</version>
162+
</dependency>
163+
164+
<!--ApiBoot & Aliyun Oss-->
151165
<dependency>
152166
<groupId>com.aliyun.oss</groupId>
153167
<artifactId>aliyun-sdk-oss</artifactId>
@@ -159,10 +173,20 @@
159173
<version>${project.version}</version>
160174
</dependency>
161175

162-
<!--ApiBoot Plugin-->
176+
<!--ApiBoot & Aliyun Sms-->
177+
<dependency>
178+
<groupId>com.aliyun</groupId>
179+
<artifactId>aliyun-java-sdk-core</artifactId>
180+
<version>${aliyun.sdk.core.version}</version>
181+
</dependency>
182+
<dependency>
183+
<groupId>com.aliyun</groupId>
184+
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
185+
<version>${aliyun.sdk.dysmsapi.version}</version>
186+
</dependency>
163187
<dependency>
164188
<groupId>org.minbox.framework</groupId>
165-
<artifactId>api-boot-plugin</artifactId>
189+
<artifactId>api-boot-plugin-alibaba-sms</artifactId>
166190
<version>${project.version}</version>
167191
</dependency>
168192

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>api-boot-plugins</artifactId>
7+
<groupId>org.minbox.framework</groupId>
8+
<version>2.1.1.RELEASE</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<description>ApiBoot集成阿里云国际短信服务</description>
12+
<properties>
13+
<main.basedir>${basedir}/../../..</main.basedir>
14+
</properties>
15+
<artifactId>api-boot-plugin-alibaba-sms</artifactId>
16+
17+
<dependencies>
18+
<!--ApiBoot Plugin-->
19+
<dependency>
20+
<groupId>org.minbox.framework</groupId>
21+
<artifactId>api-boot-plugin</artifactId>
22+
</dependency>
23+
<!--aliyun java jdk-->
24+
<dependency>
25+
<groupId>com.aliyun</groupId>
26+
<artifactId>aliyun-java-sdk-core</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.aliyun</groupId>
30+
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
31+
</dependency>
32+
</dependencies>
33+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package org.minbox.framework.api.boot.plugin.sms;
2+
3+
import com.aliyuncs.DefaultAcsClient;
4+
import com.aliyuncs.IAcsClient;
5+
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
6+
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
7+
import com.aliyuncs.profile.DefaultProfile;
8+
import com.aliyuncs.profile.IClientProfile;
9+
import lombok.AllArgsConstructor;
10+
import org.minbox.framework.api.boot.common.exception.ApiBootException;
11+
import org.minbox.framework.api.boot.plugin.sms.request.ApiBootSmsRequest;
12+
import org.minbox.framework.api.boot.plugin.sms.response.ApiBootSmsResponse;
13+
14+
import javax.annotation.PostConstruct;
15+
16+
/**
17+
* ApiBoot 阿里云 短信服务
18+
* 通过该注入该类实现发送短信
19+
*
20+
* @author:恒宇少年 - 于起宇
21+
* <p>
22+
* DateTime:2019-03-22 11:13
23+
* Blog:http://blog.yuqiyu.com
24+
* WebSite:http://www.jianshu.com/u/092df3f77bca
25+
* Gitee:https://gitee.com/hengboy
26+
* GitHub:https://github.com/hengyuboy
27+
*/
28+
@AllArgsConstructor
29+
public class ApiBootAliYunSmsService implements ApiBootSmsService {
30+
31+
/**
32+
* 阿里云国际短信产品名称
33+
*/
34+
private static final String ALIYUN_PRODUCT = "Dysmsapi";
35+
/**
36+
* 阿里云国际短信产品域名
37+
*/
38+
private static final String ALIYUN_PRODUCT_DOMAIN = "dysmsapi.aliyuncs.com";
39+
/**
40+
* 发送成功后返回code
41+
*/
42+
private static final String SUCCESS_RESULT = "OK";
43+
/**
44+
* RAM账号的AccessKey ID
45+
*/
46+
private String accessKeyId;
47+
/**
48+
* RAM账号Access Key Secret
49+
*/
50+
private String accessKeySecret;
51+
/**
52+
* 短信签名
53+
*/
54+
private String signName;
55+
/**
56+
* 短信环境
57+
*/
58+
private String profile;
59+
/**
60+
* 短信发送连接超时时长
61+
*/
62+
private long connectionTimeout;
63+
/**
64+
* 短信接收消息连接超时时长
65+
*/
66+
private long readTimeout;
67+
68+
@PostConstruct
69+
public void _init() {
70+
System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(this.connectionTimeout));
71+
System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(this.readTimeout));
72+
}
73+
74+
/**
75+
* 发送短信逻辑处理
76+
*
77+
* @param request 请求对象
78+
* @return 响应
79+
* @throws ApiBootException 异常信息
80+
*/
81+
@Override
82+
public ApiBootSmsResponse send(ApiBootSmsRequest request) throws ApiBootException {
83+
try {
84+
IClientProfile profile = DefaultProfile.getProfile(this.profile, this.accessKeyId, this.accessKeySecret);
85+
DefaultProfile.addEndpoint(this.profile, ALIYUN_PRODUCT, ALIYUN_PRODUCT_DOMAIN);
86+
IAcsClient acsClient = new DefaultAcsClient(profile);
87+
88+
SendSmsRequest sendSmsRequest = new SendSmsRequest();
89+
sendSmsRequest.setPhoneNumbers(request.getPhone());
90+
sendSmsRequest.setSignName(this.signName);
91+
sendSmsRequest.setTemplateCode(request.getTemplateCode());
92+
sendSmsRequest.setTemplateParam(request.getParam().getParamJson());
93+
94+
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(sendSmsRequest);
95+
96+
return ApiBootSmsResponse.builder().success(SUCCESS_RESULT.equals(sendSmsResponse.getCode())).build();
97+
98+
} catch (Exception e) {
99+
throw new ApiBootException("短信验证码发送送异常" + e.getMessage());
100+
}
101+
}
102+
}

api-boot-project/api-boot-plugins/api-boot-plugin/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<groupId>org.minbox.framework</groupId>
2222
<artifactId>api-boot-common</artifactId>
2323
</dependency>
24+
<!--FastJson-->
25+
<dependency>
26+
<groupId>com.alibaba</groupId>
27+
<artifactId>fastjson</artifactId>
28+
</dependency>
2429
<!--lombok-->
2530
<dependency>
2631
<groupId>org.projectlombok</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.minbox.framework.api.boot.plugin.sms;
2+
3+
import org.minbox.framework.api.boot.common.exception.ApiBootException;
4+
import org.minbox.framework.api.boot.plugin.sms.request.ApiBootSmsRequest;
5+
import org.minbox.framework.api.boot.plugin.sms.response.ApiBootSmsResponse;
6+
7+
/**
8+
* ApiBoot集成短信服务接口定义
9+
*
10+
* @author:恒宇少年 - 于起宇
11+
* <p>
12+
* DateTime:2019-03-22 11:20
13+
* Blog:http://blog.yuqiyu.com
14+
* WebSite:http://www.jianshu.com/u/092df3f77bca
15+
* Gitee:https://gitee.com/hengboy
16+
* GitHub:https://github.com/hengyuboy
17+
*/
18+
public interface ApiBootSmsService {
19+
/**
20+
* 发送短信
21+
*
22+
* @param request 请求对象
23+
* @return 发送短信响应
24+
* @throws ApiBootException 异常信息
25+
*/
26+
ApiBootSmsResponse send(ApiBootSmsRequest request) throws ApiBootException;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.minbox.framework.api.boot.plugin.sms.request;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
/**
7+
* ApiBoot 发送短信时请求实体
8+
*
9+
* @author:恒宇少年 - 于起宇
10+
* <p>
11+
* DateTime:2019-03-22 11:24
12+
* Blog:http://blog.yuqiyu.com
13+
* WebSite:http://www.jianshu.com/u/092df3f77bca
14+
* Gitee:https://gitee.com/hengboy
15+
* GitHub:https://github.com/hengyuboy
16+
*/
17+
@Data
18+
@Builder
19+
public class ApiBootSmsRequest {
20+
/**
21+
* 接收手机号
22+
*/
23+
private String phone;
24+
/**
25+
* 模板Code
26+
* 对应阿里云控制台信息
27+
*/
28+
private String templateCode;
29+
/**
30+
* 对应模板内的参数列表
31+
*/
32+
private ApiBootSmsRequestParam param = new ApiBootSmsRequestParam();
33+
}

0 commit comments

Comments
 (0)