Initial commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>oneone-common</artifactId>
|
||||
<groupId>com.oneone.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>common-sms</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.oneone.cloud</groupId>
|
||||
<artifactId>common-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.oneone.cloud</groupId>
|
||||
<artifactId>common-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.oneone.common.sms;
|
||||
|
||||
import com.oneone.common.redis.utils.RedisUtils;
|
||||
import com.oneone.common.sms.config.AliyunSmsGYProperties;
|
||||
import com.oneone.common.sms.config.AliyunSmsProperties;
|
||||
import com.oneone.common.sms.service.iml.AliyunSmsService;
|
||||
import com.oneone.common.sms.service.iml.GYSmsService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2022-03-22 17:58
|
||||
*/
|
||||
@ConditionalOnClass(StringRedisTemplate.class)
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@EnableConfigurationProperties({AliyunSmsProperties.class,AliyunSmsGYProperties.class})
|
||||
public class SmsAutoConfiguration {
|
||||
|
||||
private final AliyunSmsProperties aliyunSmsProperties;
|
||||
private final AliyunSmsGYProperties aliyunSmsGYProperties;
|
||||
private final RedisUtils redisUtils;
|
||||
|
||||
|
||||
@ConditionalOnProperty(prefix = "aliyun.sms", name = "enabled",
|
||||
havingValue = "official",matchIfMissing = false)
|
||||
@Bean
|
||||
public AliyunSmsService aliyunSmsService(){
|
||||
return new AliyunSmsService(aliyunSmsProperties,redisUtils);
|
||||
}
|
||||
|
||||
@ConditionalOnProperty(prefix = "aliyun.sms", name = "enabled",
|
||||
havingValue = "gy")
|
||||
@Bean
|
||||
public GYSmsService gySmsService(){
|
||||
return new GYSmsService(aliyunSmsGYProperties,redisUtils);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
package com.oneone.common.sms.common;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
/**
|
||||
* get
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doGet(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpGet request = new HttpGet(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* post form
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param bodys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
Map<String, String> bodys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (bodys != null) {
|
||||
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
|
||||
|
||||
for (String key : bodys.keySet()) {
|
||||
nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
|
||||
}
|
||||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
|
||||
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
|
||||
request.setEntity(formEntity);
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post String
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post stream
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put String
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put stream
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doDelete(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
|
||||
StringBuilder sbUrl = new StringBuilder();
|
||||
sbUrl.append(host);
|
||||
if (!StringUtils.isBlank(path)) {
|
||||
sbUrl.append(path);
|
||||
}
|
||||
if (null != querys) {
|
||||
StringBuilder sbQuery = new StringBuilder();
|
||||
for (Map.Entry<String, String> query : querys.entrySet()) {
|
||||
if (0 < sbQuery.length()) {
|
||||
sbQuery.append("&");
|
||||
}
|
||||
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append(query.getValue());
|
||||
}
|
||||
if (!StringUtils.isBlank(query.getKey())) {
|
||||
sbQuery.append(query.getKey());
|
||||
if (!StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append("=");
|
||||
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (0 < sbQuery.length()) {
|
||||
sbUrl.append("?").append(sbQuery);
|
||||
}
|
||||
}
|
||||
|
||||
return sbUrl.toString();
|
||||
}
|
||||
|
||||
private static HttpClient wrapClient(String host) {
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
if (host.startsWith("https://")) {
|
||||
sslClient(httpClient);
|
||||
}
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
private static void sslClient(HttpClient httpClient) {
|
||||
try {
|
||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||
X509TrustManager tm = new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
};
|
||||
ctx.init(null, new TrustManager[] { tm }, null);
|
||||
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
|
||||
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||
ClientConnectionManager ccm = httpClient.getConnectionManager();
|
||||
SchemeRegistry registry = ccm.getSchemeRegistry();
|
||||
registry.register(new Scheme("https", 443, ssf));
|
||||
} catch (KeyManagementException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.oneone.common.sms.common;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author oneone
|
||||
* @description
|
||||
* @createTime 2021/6/5 17:57
|
||||
*/
|
||||
|
||||
public enum SmsTypeEnum {
|
||||
|
||||
LOGIN("login","登录"),
|
||||
SET_OPERATE_PWD("setOperatePwd","设置操作密码"),
|
||||
CONFIRM_MOBILE("confirmMobile","确认手机号"),
|
||||
SET_MOBILE("setMobile","设置手机号"),
|
||||
|
||||
;
|
||||
|
||||
public static SmsTypeEnum getByCode(String code){
|
||||
for (SmsTypeEnum value : values()) {
|
||||
if (value.getCode().equals(code)){
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Getter
|
||||
private String code;
|
||||
@Getter
|
||||
private String desc;
|
||||
|
||||
SmsTypeEnum(String code, String desc){
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.oneone.common.sms.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author oneone
|
||||
* @date 2021/10/13 22:44
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "aliyun.sms.gy")
|
||||
@Configuration
|
||||
@Data
|
||||
public class AliyunSmsGYProperties {
|
||||
|
||||
private Integer valid;
|
||||
|
||||
private Boolean test = false;
|
||||
|
||||
private String appcode;
|
||||
|
||||
private String smsSignId;
|
||||
|
||||
private Map<String,String> templateMap;
|
||||
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.oneone.common.sms.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author oneone
|
||||
* @date 2021/10/13 22:44
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "aliyun.sms.official")
|
||||
@Configuration
|
||||
@Data
|
||||
public class AliyunSmsProperties {
|
||||
|
||||
private Integer valid;
|
||||
|
||||
private Boolean test = false;
|
||||
|
||||
private String regionId;
|
||||
|
||||
private String accessKeyId;
|
||||
|
||||
private String secret;
|
||||
|
||||
private Map<String, String> templateMap;
|
||||
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.oneone.common.sms.service;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.oneone.common.constant.RedisConstants;
|
||||
import com.oneone.common.constant.SecurityConstants;
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import com.oneone.common.redis.utils.RedisUtils;
|
||||
import com.oneone.common.result.Result;
|
||||
import com.oneone.common.result.ResultCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2022-04-06 20:19
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class AbstractSendCode implements SmsService{
|
||||
|
||||
protected RedisUtils redisUtils;
|
||||
|
||||
public void validate(String mobile,String code,String type){
|
||||
if (isTest() && "666666".equals(code)){
|
||||
return;
|
||||
}
|
||||
String codeKey = SecurityConstants.SMS_CODE_PREFIX + type + mobile;
|
||||
String correctCode = String.valueOf(redisUtils.get(codeKey));
|
||||
// 验证码比对
|
||||
if (StrUtil.isBlank(correctCode) || !code.equals(correctCode)) {
|
||||
log.error("验证码验证失败,手机号:{},验证码:{},类型:{}",mobile,code,type);
|
||||
throw new BusinessException(ResultCode.VALIDATE_CODE_ERROR);
|
||||
}
|
||||
// 比对成功删除缓存的验证码
|
||||
redisUtils.del(codeKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片验证码 验证
|
||||
* @param key
|
||||
* @param validateCode
|
||||
* @return
|
||||
*/
|
||||
public boolean imageValidate(String key,String validateCode){
|
||||
String redisKey = RedisConstants.IMAGE_VALIDATE_CODE.concat(key);
|
||||
String code = (String) redisUtils.get(redisKey);
|
||||
log.info("正确验证码:{},用户输入验证码:{}",code,validateCode);
|
||||
if (StringUtils.isEmpty(code)){
|
||||
throw new BusinessException(ResultCode.IMAGE_VALIDATE_CODE_ERROR);
|
||||
}
|
||||
if (code.equals(validateCode)){
|
||||
redisUtils.del(redisKey);
|
||||
return true;
|
||||
}else {
|
||||
redisUtils.del(redisKey);
|
||||
throw new BusinessException(ResultCode.IMAGE_VALIDATE_CODE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract boolean isTest();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.oneone.common.sms.service;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2022-03-22 17:55
|
||||
*/
|
||||
public interface SmsService {
|
||||
|
||||
boolean sendSmsCode(String phoneNumber,String type);
|
||||
|
||||
void validate(String mobile,String code,String type);
|
||||
|
||||
boolean imageValidate(String key,String validateCode);
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package com.oneone.common.sms.service.iml;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.aliyuncs.exceptions.ServerException;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.Gson;
|
||||
import com.oneone.common.constant.SecurityConstants;
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import com.oneone.common.redis.lock.Lock;
|
||||
import com.oneone.common.redis.utils.RedisUtils;
|
||||
import com.oneone.common.sms.common.HttpUtils;
|
||||
import com.oneone.common.sms.config.AliyunSmsProperties;
|
||||
import com.oneone.common.sms.service.AbstractSendCode;
|
||||
import com.oneone.common.util.JsonUtils;
|
||||
import com.oneone.common.web.util.IPUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpResponse;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 阿里云短信业务类
|
||||
*
|
||||
* @author oneone
|
||||
* @date 2022/10/13 23:04
|
||||
*/
|
||||
@Slf4j
|
||||
public class AliyunSmsService extends AbstractSendCode {
|
||||
|
||||
private AliyunSmsProperties aliyunSmsProperties;
|
||||
|
||||
public AliyunSmsService(AliyunSmsProperties aliyunSmsProperties, RedisUtils redisUtils) {
|
||||
this.aliyunSmsProperties = aliyunSmsProperties;
|
||||
this.redisUtils = redisUtils;
|
||||
}
|
||||
|
||||
//https://market.aliyun.com/products/57126001/cmapi00037415.html?spm=5176.730005.result.6.3612123eeGY9ER&innerSource=search_%E4%B8%89%E7%BD%91%E7%9F%AD%E4%BF%A1%E6%8E%A5%E5%8F%A3#sku=yuncode3141500001
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @param phoneNumber 手机号
|
||||
* @return
|
||||
*/
|
||||
@Lock(key = "sms_code", spel = "#phoneNumber")
|
||||
public boolean sendSmsCode(String phoneNumber, String type) {
|
||||
String ip = IPUtils.getIpAddrByServlet();
|
||||
Integer times = (Integer) redisUtils.get("sendSmsCode:" + ip);
|
||||
if (times != null && times > 20) {
|
||||
throw new BusinessException("今日短信次数已达上限");
|
||||
}
|
||||
String templateId = aliyunSmsProperties.getTemplateMap().get(type);
|
||||
if (templateId == null) {
|
||||
log.error("短信模板不存在");
|
||||
throw new BusinessException("获取验证码失败");
|
||||
}
|
||||
String code = RandomUtil.randomNumbers(6); // 随机生成6位的验证码
|
||||
|
||||
if (isTest()) {
|
||||
log.info("开发模式 不发短信,类型:{},手机号:{},验证码:{}", type, phoneNumber, code);
|
||||
redisUtils.set(SecurityConstants.SMS_CODE_PREFIX + type + phoneNumber, code, aliyunSmsProperties.getValid() * 60, TimeUnit.SECONDS);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
DefaultProfile profile = DefaultProfile.getProfile(aliyunSmsProperties.getRegionId(), aliyunSmsProperties.getAccessKeyId(), aliyunSmsProperties.getSecret());
|
||||
|
||||
IAcsClient client = new DefaultAcsClient(profile);
|
||||
|
||||
SendSmsRequest request = new SendSmsRequest();
|
||||
request.setSignName("文趣星球");
|
||||
request.setTemplateCode(templateId);
|
||||
request.setPhoneNumbers(phoneNumber);
|
||||
request.setTemplateParam(JsonUtils.toJSONString(ImmutableMap.of("code", code)));
|
||||
|
||||
try {
|
||||
SendSmsResponse response = client.getAcsResponse(request);
|
||||
if (response.getCode().equals("OK")) {
|
||||
return true;
|
||||
}
|
||||
log.warn("短信发送失败,类型:{},手机号:{},验证码:{},错误信息:{}", type, phoneNumber, code, response.getMessage());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error("短信发送失败,类型:{},手机号:{},验证码:{}", type, phoneNumber, code);
|
||||
}
|
||||
throw new BusinessException("获取验证码失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTest() {
|
||||
return aliyunSmsProperties.getTest();
|
||||
}
|
||||
}
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
package com.oneone.common.sms.service.iml;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.oneone.common.constant.SecurityConstants;
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import com.oneone.common.redis.lock.Lock;
|
||||
import com.oneone.common.redis.utils.RedisUtils;
|
||||
import com.oneone.common.sms.common.HttpUtils;
|
||||
import com.oneone.common.sms.config.AliyunSmsGYProperties;
|
||||
import com.oneone.common.sms.config.AliyunSmsProperties;
|
||||
import com.oneone.common.sms.service.AbstractSendCode;
|
||||
import com.oneone.common.web.util.IPUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpResponse;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 阿里云短信业务类
|
||||
*
|
||||
* @author oneone
|
||||
* @date 2022/10/13 23:04
|
||||
*/
|
||||
@Slf4j
|
||||
public class GYSmsService extends AbstractSendCode {
|
||||
|
||||
private AliyunSmsGYProperties aliyunSmsGYProperties;
|
||||
|
||||
public GYSmsService(AliyunSmsGYProperties aliyunSmsGYProperties, RedisUtils redisUtils) {
|
||||
this.aliyunSmsGYProperties = aliyunSmsGYProperties;
|
||||
this.redisUtils = redisUtils;
|
||||
}
|
||||
|
||||
//https://market.aliyun.com/products/57126001/cmapi00037415.html?spm=5176.730005.result.6.3612123eeGY9ER&innerSource=search_%E4%B8%89%E7%BD%91%E7%9F%AD%E4%BF%A1%E6%8E%A5%E5%8F%A3#sku=yuncode3141500001
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @param phoneNumber 手机号
|
||||
* @return
|
||||
*/
|
||||
@Lock(key = "sms_code", spel = "#phoneNumber")
|
||||
public boolean sendSmsCode(String phoneNumber, String type) {
|
||||
String ip = IPUtils.getIpAddrByServlet();
|
||||
Integer times = (Integer) redisUtils.get("sendSmsCode:" + ip);
|
||||
if (times != null && times > 20) {
|
||||
throw new BusinessException("今日短信次数已达上限");
|
||||
}
|
||||
String templateId = aliyunSmsGYProperties.getTemplateMap().get(type);
|
||||
if (templateId == null) {
|
||||
log.error("短信模板不存在");
|
||||
throw new BusinessException("获取验证码失败");
|
||||
}
|
||||
String code = RandomUtil.randomNumbers(6); // 随机生成6位的验证码
|
||||
|
||||
if (isTest()) {
|
||||
log.info("开发模式 不发短信,类型:{},手机号:{},验证码:{}", type, phoneNumber, code);
|
||||
redisUtils.set(SecurityConstants.SMS_CODE_PREFIX + type + phoneNumber, code, aliyunSmsGYProperties.getValid() * 60, TimeUnit.SECONDS);
|
||||
return true;
|
||||
}
|
||||
|
||||
String host = "https://gyytz.market.alicloudapi.com";
|
||||
String path = "/sms/smsSend";
|
||||
String method = "POST";
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
|
||||
headers.put("Authorization", "APPCODE " + aliyunSmsGYProperties.getAppcode());
|
||||
Map<String, String> querys = new HashMap<String, String>();
|
||||
querys.put("mobile", phoneNumber);
|
||||
querys.put("param", "**code**:" + code + ",**minute**:" + aliyunSmsGYProperties.getValid());
|
||||
querys.put("smsSignId", aliyunSmsGYProperties.getSmsSignId());
|
||||
querys.put("templateId", templateId);
|
||||
Map<String, String> bodys = new HashMap<String, String>();
|
||||
|
||||
try {
|
||||
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
|
||||
int status = response.getStatusLine().getStatusCode();
|
||||
if (status == 403) {
|
||||
// TODO告警
|
||||
log.error("阿里云短信服务403 库存已耗尽");
|
||||
throw new BusinessException("获取验证码失败");
|
||||
}
|
||||
String result = IoUtil.readUtf8(response.getEntity().getContent());
|
||||
log.info("阿里云短信服务返回:{}", result);
|
||||
JSONObject r = JSONObject.parseObject(result);
|
||||
String resultCode = r.getString("code");
|
||||
if ("0".equals(resultCode)) {
|
||||
log.info("短信发送成功,类型:{},手机号:{},验证码:{}", type, phoneNumber, code);
|
||||
redisUtils.set(SecurityConstants.SMS_CODE_PREFIX + type + phoneNumber, code, aliyunSmsGYProperties.getValid() * 60, TimeUnit.SECONDS);
|
||||
redisUtils.incr("sendSmsCode:" + ip,1);
|
||||
redisUtils.expire("sendSmsCode:" + ip, Duration.between(LocalDateTime.now(), LocalDateTime.of(LocalDate.now(), LocalTime.MAX)).getSeconds(), TimeUnit.SECONDS);
|
||||
return true;
|
||||
} else if ("1403".equals(resultCode)) {
|
||||
throw new BusinessException("手机号码不正确");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new BusinessException("获取验证码失败");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isTest() {
|
||||
return aliyunSmsGYProperties.getTest();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.oneone.common.sms.SmsAutoConfiguration
|
||||
Reference in New Issue
Block a user