From e1ae1eac2ce91318b352174a32b71a1175c6e49b Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Tue, 10 Mar 2020 15:54:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=9F=AD=E4=BF=A1=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=EF=BC=8C=E6=96=B0=E5=A2=9E=E9=BB=98=E8=AE=A4=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E5=92=8C=E7=9F=AD=E4=BF=A1=E7=B1=BB=E5=9E=8B=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../properties/SmsDefaultProperties.java | 66 +++++++++++++++++++ .../SmsDefaultTemplateProperties.java | 33 ++++++++++ .../config/properties/SmsProperties.java | 49 +++++++------- .../properties/SmsTencentProperties.java | 66 +++++++++++++++++++ .../manager/VerificationCodeManager.java | 33 +++++++++- .../impl/VerificationCodeServiceImpl.java | 52 +++++++++++++-- .../utils/DefaultSmsUtil.java | 37 +++++++++++ 7 files changed, 301 insertions(+), 35 deletions(-) create mode 100644 cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsDefaultProperties.java create mode 100644 cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsDefaultTemplateProperties.java create mode 100644 cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsTencentProperties.java create mode 100644 cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/utils/DefaultSmsUtil.java diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsDefaultProperties.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsDefaultProperties.java new file mode 100644 index 0000000..9295243 --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsDefaultProperties.java @@ -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(); + } +} diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsDefaultTemplateProperties.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsDefaultTemplateProperties.java new file mode 100644 index 0000000..62c280f --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsDefaultTemplateProperties.java @@ -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(); + } +} diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsProperties.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsProperties.java index 665b61c..a25d56b 100644 --- a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsProperties.java +++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsProperties.java @@ -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(); } } diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsTencentProperties.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsTencentProperties.java new file mode 100644 index 0000000..eaedf71 --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsTencentProperties.java @@ -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(); + } +} diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java index 50c7803..ca25710 100644 --- a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java +++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java @@ -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); + } + /** * 删除过期验证码 */ diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java index 9bfe66d..6a47df1 100644 --- a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java +++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java @@ -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); diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/utils/DefaultSmsUtil.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/utils/DefaultSmsUtil.java new file mode 100644 index 0000000..0df70c6 --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/utils/DefaultSmsUtil.java @@ -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("验证码发送失败"); + } + } + +}