From 9d0a27fc9697c87a988eda5e6c69f377c421fee7 Mon Sep 17 00:00:00 2001
From: wenc000 <450292408@qq.com>
Date: Mon, 9 Mar 2020 23:25:43 +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=E8=85=BE=E8=AE=AF=E7=9F=AD?=
=?UTF-8?q?=E4=BF=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../pojo/WechatOfficialAccountProperties.java | 13 +++-
.../cm/common/wechat/filter/WechatFilter.java | 34 +++++++--
.../common/wechat/startup/WechatStartUp.java | 8 ++
.../com/cm/common/wechat/task/WechatTask.java | 10 +++
.../pom.xml | 8 +-
.../config/properties/SmsProperties.java | 65 +++++++++++++++++
.../VerificationCodeController.java | 10 +--
.../manager/VerificationCodeManager.java | 0
.../pojo/VerificationCode.java | 0
.../service/IVerificationCodeService.java | 5 +-
.../impl/VerificationCodeServiceImpl.java | 73 +++++++++++++++++++
.../task/VerificationCodeTask.java | 0
.../impl/VerificationCodeServiceImpl.java | 35 ---------
pom.xml | 2 +-
14 files changed, 209 insertions(+), 54 deletions(-)
rename {cloud-manager-verification-code => cloud-manager-sms}/pom.xml (75%)
create mode 100644 cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsProperties.java
rename {cloud-manager-verification-code => cloud-manager-sms}/src/main/java/com/cm/manager/verificationcode/controller/VerificationCodeController.java (78%)
rename {cloud-manager-verification-code => cloud-manager-sms}/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java (100%)
rename {cloud-manager-verification-code => cloud-manager-sms}/src/main/java/com/cm/manager/verificationcode/pojo/VerificationCode.java (100%)
rename {cloud-manager-verification-code => cloud-manager-sms}/src/main/java/com/cm/manager/verificationcode/service/IVerificationCodeService.java (84%)
create mode 100644 cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java
rename {cloud-manager-verification-code => cloud-manager-sms}/src/main/java/com/cm/manager/verificationcode/task/VerificationCodeTask.java (100%)
delete mode 100644 cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java
diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/config/pojo/WechatOfficialAccountProperties.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/config/pojo/WechatOfficialAccountProperties.java
index 012ba42..ccce16d 100644
--- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/config/pojo/WechatOfficialAccountProperties.java
+++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/config/pojo/WechatOfficialAccountProperties.java
@@ -18,6 +18,7 @@ import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "open-platform.wechat.official-account")
public class WechatOfficialAccountProperties {
+ private Boolean activate;
private WechatOfficialAccountAuthorizeProperties authorize;
private String accessTokenUrl;
private String bindUserUrl;
@@ -26,6 +27,14 @@ public class WechatOfficialAccountProperties {
private String grantType;
private String configToken;
+ public Boolean getActivate() {
+ return activate;
+ }
+
+ public void setActivate(Boolean activate) {
+ this.activate = activate;
+ }
+
public WechatOfficialAccountAuthorizeProperties getAuthorize() {
return authorize;
}
@@ -85,7 +94,9 @@ public class WechatOfficialAccountProperties {
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
- sb.append("\"authorize\":")
+ sb.append("\"activate\":")
+ .append(activate);
+ sb.append(",\"authorize\":")
.append(authorize);
sb.append(",\"accessTokenUrl\":")
.append("\"").append(accessTokenUrl).append("\"");
diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java
index 9c9c243..4ef595f 100644
--- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java
+++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java
@@ -79,16 +79,36 @@ public class WechatFilter implements Filter {
// 如果header存在token则校验token,如果没有就校验session
String token = request.getHeader("token");
if (!StringUtils.isEmpty(token)) {
- try {
- checkToken(token);
- } catch (TokenException e) {
- errorResponse(response, e.getMessage());
- return;
- }
- filterChain.doFilter(request, response);
+ tokenFilter(token, request, response, filterChain);
return;
}
+ // 公众号激活有效
+ if (wechatOfficialAccountProperties.getActivate() != null && wechatOfficialAccountProperties.getActivate()) {
+ wechatOfficialAccountFilter(request, response, filterChain);
+ return;
+ }
+
+ errorResponse(response, "token不能为空");
+ }
+
+ private void tokenFilter(String token, HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
+ try {
+ checkToken(token);
+ } catch (TokenException e) {
+ errorResponse(response, e.getMessage());
+ return;
+ }
+ filterChain.doFilter(request, response);
+ }
+
+ /**
+ * 微信公众号自动授权
+ *
+ * @param request
+ * @param response
+ */
+ private void wechatOfficialAccountFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
// 判断是不是微信配置校验
String signatureParameter = request.getParameter("signature");
String timestampParameter = request.getParameter("timestamp");
diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/startup/WechatStartUp.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/startup/WechatStartUp.java
index 05a3ce0..9ee5e7b 100644
--- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/startup/WechatStartUp.java
+++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/startup/WechatStartUp.java
@@ -2,6 +2,8 @@ package com.cm.common.wechat.startup;
import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
import com.cm.common.wechat.manager.officialaccount.WechatOfficialAccountManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
@@ -20,11 +22,17 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class WechatStartUp implements ApplicationRunner {
+ private static final Logger LOG = LoggerFactory.getLogger(WechatStartUp.class);
+
@Autowired
private WechatOfficialAccountProperties wechatOfficialAccountProperties;
@Override
public void run(ApplicationArguments args) throws Exception {
+ if (wechatOfficialAccountProperties.getActivate() == null || !wechatOfficialAccountProperties.getActivate()) {
+ LOG.info("微信公众号未激活");
+ return;
+ }
// 初始化公众号AccessToken
WechatOfficialAccountManager wechatOfficialAccountManager = WechatOfficialAccountManager.getInstance();
wechatOfficialAccountManager.setWechatOfficialAccountProperties(wechatOfficialAccountProperties);
diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/task/WechatTask.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/task/WechatTask.java
index 462b2b4..f32ef79 100644
--- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/task/WechatTask.java
+++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/task/WechatTask.java
@@ -2,6 +2,8 @@ package com.cm.common.wechat.task;
import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
import com.cm.common.wechat.manager.officialaccount.WechatOfficialAccountManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -21,11 +23,19 @@ import org.springframework.scheduling.annotation.Scheduled;
@EnableScheduling
public class WechatTask {
+ private static final Logger LOG = LoggerFactory.getLogger(WechatTask.class);
+ @Autowired
+ private WechatOfficialAccountProperties wechatOfficialAccountProperties;
+
/**
* 刷新公众号AccessToken,每10分钟刷新一次
*/
@Scheduled(cron = "0 0/10 * * * ?")
public void refreshOfficialAccountAccessToken() {
+ if (wechatOfficialAccountProperties.getActivate() == null || !wechatOfficialAccountProperties.getActivate()) {
+ LOG.info("微信公众号未激活,取消刷新");
+ return;
+ }
WechatOfficialAccountManager.getInstance().refreshAccessToken();
}
diff --git a/cloud-manager-verification-code/pom.xml b/cloud-manager-sms/pom.xml
similarity index 75%
rename from cloud-manager-verification-code/pom.xml
rename to cloud-manager-sms/pom.xml
index ef1af8f..290396e 100644
--- a/cloud-manager-verification-code/pom.xml
+++ b/cloud-manager-sms/pom.xml
@@ -10,7 +10,7 @@
4.0.0
验证码模块
- cloud-manager-verification-code
+ cloud-manager-sms
@@ -18,6 +18,12 @@
cloud-common
1.0.1-SNAPSHOT
+
+
+ com.github.qcloudsms
+ qcloudsms
+ 1.0.6
+
\ No newline at end of file
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
new file mode 100644
index 0000000..665b61c
--- /dev/null
+++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/config/properties/SmsProperties.java
@@ -0,0 +1,65 @@
+package com.cm.manager.verificationcode.config.properties;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * When you feel like quitting. Think about why you started
+ * 当你想要放弃的时候,想想当初你为何开始
+ *
+ * @ClassName: SmsConfigProperties
+ * @Description: 短信服务配置
+ * @Author: WangGeng
+ * @Date: 2020/3/9 10:14 下午
+ * @Version: 1.0
+ **/
+@Configuration
+@ConfigurationProperties(prefix = "sms")
+public class SmsProperties {
+
+ private String type;
+ private String appId;
+ private String appKey;
+ private String smsSign;
+ private String verificationCodeTemplateId;
+
+ public String getType() {
+ return type == null ? "" : type.trim();
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ 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;
+ }
+}
diff --git a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/controller/VerificationCodeController.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/controller/VerificationCodeController.java
similarity index 78%
rename from cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/controller/VerificationCodeController.java
rename to cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/controller/VerificationCodeController.java
index cb7b218..dec441f 100644
--- a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/controller/VerificationCodeController.java
+++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/controller/VerificationCodeController.java
@@ -4,6 +4,7 @@ import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.ParamsException;
import com.cm.common.result.SuccessResult;
import com.cm.common.utils.RegexUtil;
+import com.cm.manager.verificationcode.config.properties.SmsProperties;
import com.cm.manager.verificationcode.manager.VerificationCodeManager;
import com.cm.manager.verificationcode.service.IVerificationCodeService;
import io.swagger.annotations.Api;
@@ -32,15 +33,12 @@ public class VerificationCodeController {
@Autowired
private IVerificationCodeService verificationCodeService;
- @GetMapping("getverificationcode/{type}/{phone}")
- public SuccessResult getVerificationCode(@PathVariable("type") String type, @PathVariable("phone") String phone) {
- if (!StringUtils.equals(IVerificationCodeService.CUSTOM_TYPE, type)) {
- throw new ParamsException("类别错误");
- }
+ @GetMapping("getverificationcode/{phone}")
+ public SuccessResult getVerificationCode(@PathVariable("phone") String phone) {
if (!RegexUtil.isPhone(phone)) {
throw new ParamsException("手机号格式错误");
}
- return verificationCodeService.getVerificationCode(type, phone);
+ return verificationCodeService.getVerificationCode(phone);
}
}
diff --git a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java
similarity index 100%
rename from cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java
rename to cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/manager/VerificationCodeManager.java
diff --git a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/pojo/VerificationCode.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/pojo/VerificationCode.java
similarity index 100%
rename from cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/pojo/VerificationCode.java
rename to cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/pojo/VerificationCode.java
diff --git a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/service/IVerificationCodeService.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/IVerificationCodeService.java
similarity index 84%
rename from cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/service/IVerificationCodeService.java
rename to cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/IVerificationCodeService.java
index 8a39e8a..5050968 100644
--- a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/service/IVerificationCodeService.java
+++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/IVerificationCodeService.java
@@ -17,7 +17,7 @@ public interface IVerificationCodeService {
/**
* 腾讯类别
*/
- String TX_TYPE = "tx";
+ String TENCENT_TYPE = "tencent";
/**
* 自定义类别
*/
@@ -26,9 +26,8 @@ public interface IVerificationCodeService {
/**
* 获取验证码
*
- * @param type
* @param phone
* @return
*/
- SuccessResult getVerificationCode(String type, String phone);
+ SuccessResult getVerificationCode(String phone);
}
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
new file mode 100644
index 0000000..9bfe66d
--- /dev/null
+++ b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java
@@ -0,0 +1,73 @@
+package com.cm.manager.verificationcode.service.impl;
+
+import com.cm.common.base.AbstractService;
+import com.cm.common.exception.base.SystemException;
+import com.cm.common.result.SuccessResult;
+import com.cm.manager.verificationcode.config.properties.SmsProperties;
+import com.cm.manager.verificationcode.manager.VerificationCodeManager;
+import com.cm.manager.verificationcode.service.IVerificationCodeService;
+import com.github.qcloudsms.SmsMultiSender;
+import com.github.qcloudsms.SmsMultiSenderResult;
+import com.github.qcloudsms.httpclient.HTTPException;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+
+/**
+ * When you feel like quitting. Think about why you started
+ * 当你想要放弃的时候,想想当初你为何开始
+ *
+ * @ClassName: VerificationCodeServiceImpl
+ * @Description: 验证码
+ * @Author: WangGeng
+ * @Date: 2020/3/4 7:44 下午
+ * @Version: 1.0
+ **/
+@Service
+public class VerificationCodeServiceImpl extends AbstractService implements IVerificationCodeService {
+
+ @Autowired
+ private SmsProperties smsProperties;
+
+ @Override
+ public SuccessResult getVerificationCode(String phone) {
+ String currentTimeStr = String.valueOf(System.currentTimeMillis());
+ String code = currentTimeStr.substring(currentTimeStr.length() - 6);
+ VerificationCodeManager.getInstance().setVerificationCode(phone, code);
+ sendCode(phone, code);
+ return new SuccessResult();
+ }
+
+ private void sendCode(String phone, String code) {
+ LOG.debug("发送验证码:{}", code);
+ if (StringUtils.equals(TENCENT_TYPE, smsProperties.getType())) {
+ tencentSms(phone, code);
+ } else {
+
+ }
+ }
+
+ /**
+ * 腾讯验证码
+ *
+ * @param phone
+ * @param code
+ */
+ private void tencentSms(String phone, String code) {
+ String[] params = {code};
+ SmsMultiSender sender = new SmsMultiSender(Integer.parseInt(smsProperties.getAppId()), smsProperties.getAppKey());
+ try {
+ SmsMultiSenderResult result = sender.sendWithParam("86", new String[]{phone}, Integer.parseInt(smsProperties.getVerificationCodeTemplateId()), params, smsProperties.getSmsSign(), "", "");
+ LOG.debug("Tencent sms result: {}", result);
+ } catch (HTTPException e) {
+ LOG.error(e.getMessage(), e);
+ throw new SystemException("发送验证码失败");
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ throw new SystemException("发送验证码失败");
+ }
+ }
+
+}
diff --git a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/task/VerificationCodeTask.java b/cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/task/VerificationCodeTask.java
similarity index 100%
rename from cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/task/VerificationCodeTask.java
rename to cloud-manager-sms/src/main/java/com/cm/manager/verificationcode/task/VerificationCodeTask.java
diff --git a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java b/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java
deleted file mode 100644
index c159f2d..0000000
--- a/cloud-manager-verification-code/src/main/java/com/cm/manager/verificationcode/service/impl/VerificationCodeServiceImpl.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package com.cm.manager.verificationcode.service.impl;
-
-import com.cm.common.base.AbstractService;
-import com.cm.common.result.SuccessResult;
-import com.cm.manager.verificationcode.manager.VerificationCodeManager;
-import com.cm.manager.verificationcode.service.IVerificationCodeService;
-import org.springframework.stereotype.Service;
-
-/**
- * When you feel like quitting. Think about why you started
- * 当你想要放弃的时候,想想当初你为何开始
- *
- * @ClassName: VerificationCodeServiceImpl
- * @Description: 验证码
- * @Author: WangGeng
- * @Date: 2020/3/4 7:44 下午
- * @Version: 1.0
- **/
-@Service
-public class VerificationCodeServiceImpl extends AbstractService implements IVerificationCodeService {
-
- @Override
- public SuccessResult getVerificationCode(String type, String phone) {
- String currentTimeStr = String.valueOf(System.currentTimeMillis());
- String code = currentTimeStr.substring(currentTimeStr.length() - 6);
- VerificationCodeManager.getInstance().setVerificationCode(phone, code);
- sendCode(type, code);
- return new SuccessResult();
- }
-
- private void sendCode(String type, String code) {
- LOG.debug("发送验证码:{}", code);
- }
-
-}
diff --git a/pom.xml b/pom.xml
index 56802b4..408f4c4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -17,7 +17,7 @@
cloud-common-plugin-dynamic
cloud-common-plugin-dictionary
cloud-common-wechat
- cloud-manager-verification-code
+ cloud-manager-sms
pom
成迈云