|
@@ -4,12 +4,15 @@ import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
import com.aliyuncs.IAcsClient;
|
|
import com.aliyuncs.IAcsClient;
|
|
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
|
|
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
|
|
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
|
|
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
|
|
|
|
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
|
|
|
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
|
import com.aliyuncs.exceptions.ClientException;
|
|
import com.aliyuncs.exceptions.ClientException;
|
|
import com.aliyuncs.exceptions.ServerException;
|
|
import com.aliyuncs.exceptions.ServerException;
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
@@ -27,26 +30,34 @@ public class SendSmsServiceImpl implements SendSmsService {
|
|
@Autowired
|
|
@Autowired
|
|
private RedisClient redisClient;
|
|
private RedisClient redisClient;
|
|
|
|
|
|
|
|
+ //redis中短信验证码key的前缀
|
|
private static final String CODE_REDIS_KEY_PREFIXS = "SMSCODE_";
|
|
private static final String CODE_REDIS_KEY_PREFIXS = "SMSCODE_";
|
|
|
|
|
|
|
|
+ //验证码默认有效时间
|
|
|
|
+ private static final int DEFAULT_DFFECTIVETIME = 120;
|
|
|
|
+
|
|
|
|
+ //短信发送默认间隔时间
|
|
|
|
+ private static final int DEFAULT_INTERVALSECONDS = 90;
|
|
|
|
+
|
|
// 产品名称:云通信短信API产品,开发者无需替换
|
|
// 产品名称:云通信短信API产品,开发者无需替换
|
|
private static final String product = "Dysmsapi";
|
|
private static final String product = "Dysmsapi";
|
|
|
|
|
|
// 产品域名,开发者无需替换
|
|
// 产品域名,开发者无需替换
|
|
private static final String domain = "dysmsapi.aliyuncs.com";
|
|
private static final String domain = "dysmsapi.aliyuncs.com";
|
|
|
|
|
|
- // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
|
|
|
|
- private static final String accessKeyId = "LTAIhSyYIfGx3e9H";
|
|
|
|
-
|
|
|
|
- private static final String accessKeySecret = "NyT0UfUuP900pYhYTM92AEBYP43uZA";
|
|
|
|
|
|
+ @Value("${aliyun.sms.accessKeyId}")
|
|
|
|
+ private String accessKeyId;
|
|
|
|
+
|
|
|
|
+ @Value("${aliyun.sms.accessKeySecret}")
|
|
|
|
+ private String accessKeySecret;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void sendIdentifyingCode(SendSmsInfo sendSmsInfo) {
|
|
public void sendIdentifyingCode(SendSmsInfo sendSmsInfo) {
|
|
if(sendSmsInfo.getEffectiveTime() == null || sendSmsInfo.getEffectiveTime() == 0){
|
|
if(sendSmsInfo.getEffectiveTime() == null || sendSmsInfo.getEffectiveTime() == 0){
|
|
- sendSmsInfo.setEffectiveTime(120);//验证码默认有效时间
|
|
|
|
|
|
+ sendSmsInfo.setEffectiveTime(DEFAULT_DFFECTIVETIME);
|
|
}
|
|
}
|
|
if(sendSmsInfo.getIntervalSeconds() == null|| sendSmsInfo.getIntervalSeconds() == 0){
|
|
if(sendSmsInfo.getIntervalSeconds() == null|| sendSmsInfo.getIntervalSeconds() == 0){
|
|
- sendSmsInfo.setIntervalSeconds(90);//默认间隔时间
|
|
|
|
|
|
+ sendSmsInfo.setIntervalSeconds(DEFAULT_INTERVALSECONDS);
|
|
}
|
|
}
|
|
SmsCodeRedisInfo smsCodeRedisInfo = redisClient.get(CODE_REDIS_KEY_PREFIXS+sendSmsInfo.getPhone(),SmsCodeRedisInfo.class);
|
|
SmsCodeRedisInfo smsCodeRedisInfo = redisClient.get(CODE_REDIS_KEY_PREFIXS+sendSmsInfo.getPhone(),SmsCodeRedisInfo.class);
|
|
if(smsCodeRedisInfo != null){
|
|
if(smsCodeRedisInfo != null){
|
|
@@ -56,7 +67,7 @@ public class SendSmsServiceImpl implements SendSmsService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-/* // 可自助调整超时时间
|
|
|
|
|
|
+ // 可自助调整超时时间
|
|
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
|
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
|
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
|
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
|
|
|
|
|
@@ -85,13 +96,13 @@ public class SendSmsServiceImpl implements SendSmsService {
|
|
// request.setSmsUpExtendCode("90997");
|
|
// request.setSmsUpExtendCode("90997");
|
|
|
|
|
|
// 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
|
|
// 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
|
|
- request.setOutId("yourOutId");*/
|
|
|
|
|
|
+ request.setOutId("yourOutId");
|
|
|
|
|
|
// hint 此处可能会抛出异常,注意catch
|
|
// hint 此处可能会抛出异常,注意catch
|
|
- /*try {
|
|
|
|
|
|
+ try {
|
|
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
|
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
|
if("ok".equalsIgnoreCase(sendSmsResponse.getCode())){
|
|
if("ok".equalsIgnoreCase(sendSmsResponse.getCode())){
|
|
- putCodeInfoToRedis(sendSmsInfo);
|
|
|
|
|
|
+ setCodeInfoToRedis(sendSmsInfo);
|
|
}else{
|
|
}else{
|
|
throw new StatusException("EX-sendIdentifyingCode-2", "code:"+sendSmsResponse.getCode()+",message:"+sendSmsResponse.getMessage());
|
|
throw new StatusException("EX-sendIdentifyingCode-2", "code:"+sendSmsResponse.getCode()+",message:"+sendSmsResponse.getMessage());
|
|
}
|
|
}
|
|
@@ -99,15 +110,14 @@ public class SendSmsServiceImpl implements SendSmsService {
|
|
throw new StatusException("EX-sendIdentifyingCode-3", "aliyun send sms failed");
|
|
throw new StatusException("EX-sendIdentifyingCode-3", "aliyun send sms failed");
|
|
} catch (ClientException e) {
|
|
} catch (ClientException e) {
|
|
throw new StatusException("EX-sendIdentifyingCode-3", "aliyun send sms failed");
|
|
throw new StatusException("EX-sendIdentifyingCode-3", "aliyun send sms failed");
|
|
- }*/
|
|
|
|
- putCodeInfoToRedis(sendSmsInfo);
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 将短信验证码信息放入redis
|
|
* 将短信验证码信息放入redis
|
|
* @param sendSmsInfo
|
|
* @param sendSmsInfo
|
|
*/
|
|
*/
|
|
- private void putCodeInfoToRedis(SendSmsInfo sendSmsInfo){
|
|
|
|
|
|
+ private void setCodeInfoToRedis(SendSmsInfo sendSmsInfo){
|
|
String redisKey = CODE_REDIS_KEY_PREFIXS+sendSmsInfo.getPhone();
|
|
String redisKey = CODE_REDIS_KEY_PREFIXS+sendSmsInfo.getPhone();
|
|
long currentTime = System.currentTimeMillis();
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
|