调整短信模块,新增默认方式和短信类型配置

This commit is contained in:
wenc000 2020-03-10 15:54:51 +08:00
parent 9d0a27fc96
commit e1ae1eac2c
7 changed files with 301 additions and 35 deletions

View File

@ -0,0 +1,66 @@
package com.cm.manager.verificationcode.config.properties;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: SmsDefaultProperties
* @Description: 默认短信属性
* @Author: WangGeng
* @Date: 2020/3/10 12:55 下午
* @Version: 1.0
**/
public class SmsDefaultProperties {
private String account;
private String password;
private String sign;
private SmsDefaultTemplateProperties template;
public String getAccount() {
return account == null ? "" : account.trim();
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password == null ? "" : password.trim();
}
public void setPassword(String password) {
this.password = password;
}
public String getSign() {
return sign == null ? "" : sign.trim();
}
public void setSign(String sign) {
this.sign = sign;
}
public SmsDefaultTemplateProperties getTemplate() {
return template;
}
public void setTemplate(SmsDefaultTemplateProperties template) {
this.template = template;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"account\":")
.append("\"").append(account).append("\"");
sb.append(",\"password\":")
.append("\"").append(password).append("\"");
sb.append(",\"sign\":")
.append("\"").append(sign).append("\"");
sb.append(",\"template\":")
.append(template);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,33 @@
package com.cm.manager.verificationcode.config.properties;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: SmsDefaultTemplateProperties
* @Description: 默认短信模板属性
* @Author: WangGeng
* @Date: 2020/3/10 1:00 下午
* @Version: 1.0
**/
public class SmsDefaultTemplateProperties {
private String verificationCode;
public String getVerificationCode() {
return verificationCode == null || verificationCode.isEmpty() ? "{sign} 您的验证码为 {content}, 有效时间为120秒若非本人操作请忽略。" : verificationCode.trim();
}
public void setVerificationCode(String verificationCode) {
this.verificationCode = verificationCode;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"verificationCode\":")
.append("\"").append(verificationCode).append("\"");
sb.append('}');
return sb.toString();
}
}

View File

@ -8,7 +8,7 @@ import org.springframework.context.annotation.Configuration;
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: SmsConfigProperties
* @Description: 短信服务配置
* @Description: 短信服务属性
* @Author: WangGeng
* @Date: 2020/3/9 10:14 下午
* @Version: 1.0
@ -18,10 +18,8 @@ import org.springframework.context.annotation.Configuration;
public class SmsProperties {
private String type;
private String appId;
private String appKey;
private String smsSign;
private String verificationCodeTemplateId;
private SmsDefaultProperties defaultSms;
private SmsTencentProperties tencentSms;
public String getType() {
return type == null ? "" : type.trim();
@ -31,35 +29,32 @@ public class SmsProperties {
this.type = type;
}
public String getAppId() {
return appId == null ? "" : appId.trim();
public SmsDefaultProperties getDefaultSms() {
return defaultSms;
}
public void setAppId(String appId) {
this.appId = appId;
public void setDefaultSms(SmsDefaultProperties defaultSms) {
this.defaultSms = defaultSms;
}
public String getAppKey() {
return appKey == null ? "" : appKey.trim();
public SmsTencentProperties getTencentSms() {
return tencentSms;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
public void setTencentSms(SmsTencentProperties tencentSms) {
this.tencentSms = tencentSms;
}
public String getSmsSign() {
return smsSign == null ? "" : smsSign.trim();
}
public void setSmsSign(String smsSign) {
this.smsSign = smsSign;
}
public String getVerificationCodeTemplateId() {
return verificationCodeTemplateId == null ? "" : verificationCodeTemplateId.trim();
}
public void setVerificationCodeTemplateId(String verificationCodeTemplateId) {
this.verificationCodeTemplateId = verificationCodeTemplateId;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"type\":")
.append("\"").append(type).append("\"");
sb.append(",\"defaultSms\":")
.append(defaultSms);
sb.append(",\"tencentSms\":")
.append(tencentSms);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,66 @@
package com.cm.manager.verificationcode.config.properties;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: SmsTencentProperties
* @Description: 短信腾讯属性
* @Author: WangGeng
* @Date: 2020/3/10 1:02 下午
* @Version: 1.0
**/
public class SmsTencentProperties {
private String appId;
private String appKey;
private String smsSign;
private String verificationCodeTemplateId;
public String getAppId() {
return appId == null ? "" : appId.trim();
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppKey() {
return appKey == null ? "" : appKey.trim();
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getSmsSign() {
return smsSign == null ? "" : smsSign.trim();
}
public void setSmsSign(String smsSign) {
this.smsSign = smsSign;
}
public String getVerificationCodeTemplateId() {
return verificationCodeTemplateId == null ? "" : verificationCodeTemplateId.trim();
}
public void setVerificationCodeTemplateId(String verificationCodeTemplateId) {
this.verificationCodeTemplateId = verificationCodeTemplateId;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"appId\":")
.append("\"").append(appId).append("\"");
sb.append(",\"appKey\":")
.append("\"").append(appKey).append("\"");
sb.append(",\"smsSign\":")
.append("\"").append(smsSign).append("\"");
sb.append(",\"verificationCodeTemplateId\":")
.append("\"").append(verificationCodeTemplateId).append("\"");
sb.append('}');
return sb.toString();
}
}

View File

@ -27,7 +27,7 @@ public class VerificationCodeManager {
/**
* 有效时间60s
*/
public static final long EXPIRE_TIME = 60000L;
public static final long EXPIRE_TIME = 120000L;
private VerificationCodeManager() {
}
@ -57,12 +57,39 @@ public class VerificationCodeManager {
*/
public String getVerificationCode(String key) {
VerificationCode verificationCode = verificationCodeMap.get(key);
if (System.currentTimeMillis() - verificationCode.getTime() <= EXPIRE_TIME) {
verificationCode.getCode();
if (verificationCode != null && (System.currentTimeMillis() - verificationCode.getTime() < EXPIRE_TIME)) {
return verificationCode.getCode();
}
return null;
}
/**
* 是否有有效的验证码
*
* @param key
* @return
*/
public long effectiveVerificationCodeTimeRemaining(String key) {
VerificationCode verificationCode = verificationCodeMap.get(key);
if (verificationCode == null) {
return 0;
}
long timeRemaining = System.currentTimeMillis() - verificationCode.getTime();
if (timeRemaining >= EXPIRE_TIME) {
return 0;
}
return EXPIRE_TIME - timeRemaining;
}
/**
* 清理使用后的验证码
*
* @param key
*/
public void clearUsedVerificationCode(String key) {
verificationCodeMap.remove(key);
}
/**
* 删除过期验证码
*/

View File

@ -1,17 +1,24 @@
package com.cm.manager.verificationcode.service.impl;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.ParamsException;
import com.cm.common.exception.base.SystemException;
import com.cm.common.result.SuccessResult;
import com.cm.manager.verificationcode.config.properties.SmsDefaultProperties;
import com.cm.manager.verificationcode.config.properties.SmsProperties;
import com.cm.manager.verificationcode.config.properties.SmsTencentProperties;
import com.cm.manager.verificationcode.manager.VerificationCodeManager;
import com.cm.manager.verificationcode.service.IVerificationCodeService;
import com.cm.manager.verificationcode.utils.DefaultSmsUtil;
import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
@ -33,22 +40,56 @@ public class VerificationCodeServiceImpl extends AbstractService implements IVer
@Override
public SuccessResult getVerificationCode(String phone) {
VerificationCodeManager verificationCodeManager = VerificationCodeManager.getInstance();
long timeRemaining = verificationCodeManager.effectiveVerificationCodeTimeRemaining(phone);
if (timeRemaining > 0) {
throw new ParamsException(String.format("验证码已发送,若没有收到,请在%d秒后重试", (timeRemaining / 1000)));
}
String currentTimeStr = String.valueOf(System.currentTimeMillis());
String code = currentTimeStr.substring(currentTimeStr.length() - 6);
VerificationCodeManager.getInstance().setVerificationCode(phone, code);
sendCode(phone, code);
verificationCodeManager.setVerificationCode(phone, code);
return new SuccessResult();
}
private void sendCode(String phone, String code) {
LOG.debug("发送验证码:{}", code);
LOG.info(">>>>> 向手机号:{},发送验证码:{}", phone, code);
if (StringUtils.equals(TENCENT_TYPE, smsProperties.getType())) {
tencentSms(phone, code);
} else {
defaultSms(phone, code);
}
}
/**
* 默认验证码
*
* @param phone
* @param code
*/
private void defaultSms(String phone, String code) {
SmsDefaultProperties smsDefaultProperties = smsProperties.getDefaultSms();
String content = smsDefaultProperties.getTemplate().getVerificationCode().replace("{sign}", smsDefaultProperties.getSign()).replace("{content}", code);
String url = "https://dx.ipyy.net/sms.aspx?action=send&userid=&account={account}&password={password}&mobile={mobile}&content={content}&sendTime=&extno={extno}";
String[] paramArray = new String[]{
smsDefaultProperties.getAccount(),
smsDefaultProperties.getPassword(),
phone,
content,
phone
};
DefaultSmsUtil.sendSms(url, paramArray);
}
public static void main(String[] args) {
//
RestTemplate restTemplate = new RestTemplate();
String url = "https://dx.ipyy.net/sms.aspx?action=send&userid=&account=xd001382&password=xd001382125&mobile=18647109157&content=";
String result = restTemplate.getForObject(url, String.class);
JSONObject jsonObject = XML.toJSONObject(result);
System.out.println(jsonObject.toString());
}
/**
* 腾讯验证码
*
@ -57,9 +98,10 @@ public class VerificationCodeServiceImpl extends AbstractService implements IVer
*/
private void tencentSms(String phone, String code) {
String[] params = {code};
SmsMultiSender sender = new SmsMultiSender(Integer.parseInt(smsProperties.getAppId()), smsProperties.getAppKey());
SmsTencentProperties smsTencentProperties = smsProperties.getTencentSms();
SmsMultiSender sender = new SmsMultiSender(Integer.parseInt(smsTencentProperties.getAppId()), smsTencentProperties.getAppKey());
try {
SmsMultiSenderResult result = sender.sendWithParam("86", new String[]{phone}, Integer.parseInt(smsProperties.getVerificationCodeTemplateId()), params, smsProperties.getSmsSign(), "", "");
SmsMultiSenderResult result = sender.sendWithParam("86", new String[]{phone}, Integer.parseInt(smsTencentProperties.getVerificationCodeTemplateId()), params, smsTencentProperties.getSmsSign(), "", "");
LOG.debug("Tencent sms result: {}", result);
} catch (HTTPException e) {
LOG.error(e.getMessage(), e);

View File

@ -0,0 +1,37 @@
package com.cm.manager.verificationcode.utils;
import com.cm.common.exception.base.SystemException;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.json.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: DefaultSmsUtil
* @Description: 默认短信工具
* @Author: WangGeng
* @Date: 2020/3/10 1:53 下午
* @Version: 1.0
**/
public class DefaultSmsUtil {
private static final Logger LOG = LoggerFactory.getLogger(DefaultSmsUtil.class);
public static void sendSms(String url, String[] paramArray) {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class, paramArray);
JSONObject jsonObject = XML.toJSONObject(result);
if (StringUtils.equals("Success", jsonObject.getJSONObject("returnsms").getString("returnstatus"))) {
LOG.info("验证码发送成功");
} else {
LOG.error("验证码发送失败,原因:{}。", jsonObject.toString());
throw new SystemException("验证码发送失败");
}
}
}