调整短信模块,新增腾讯短信
This commit is contained in:
parent
198a5cd978
commit
9d0a27fc96
@ -18,6 +18,7 @@ import org.springframework.stereotype.Component;
|
|||||||
@ConfigurationProperties(prefix = "open-platform.wechat.official-account")
|
@ConfigurationProperties(prefix = "open-platform.wechat.official-account")
|
||||||
public class WechatOfficialAccountProperties {
|
public class WechatOfficialAccountProperties {
|
||||||
|
|
||||||
|
private Boolean activate;
|
||||||
private WechatOfficialAccountAuthorizeProperties authorize;
|
private WechatOfficialAccountAuthorizeProperties authorize;
|
||||||
private String accessTokenUrl;
|
private String accessTokenUrl;
|
||||||
private String bindUserUrl;
|
private String bindUserUrl;
|
||||||
@ -26,6 +27,14 @@ public class WechatOfficialAccountProperties {
|
|||||||
private String grantType;
|
private String grantType;
|
||||||
private String configToken;
|
private String configToken;
|
||||||
|
|
||||||
|
public Boolean getActivate() {
|
||||||
|
return activate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActivate(Boolean activate) {
|
||||||
|
this.activate = activate;
|
||||||
|
}
|
||||||
|
|
||||||
public WechatOfficialAccountAuthorizeProperties getAuthorize() {
|
public WechatOfficialAccountAuthorizeProperties getAuthorize() {
|
||||||
return authorize;
|
return authorize;
|
||||||
}
|
}
|
||||||
@ -85,7 +94,9 @@ public class WechatOfficialAccountProperties {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuilder sb = new StringBuilder("{");
|
final StringBuilder sb = new StringBuilder("{");
|
||||||
sb.append("\"authorize\":")
|
sb.append("\"activate\":")
|
||||||
|
.append(activate);
|
||||||
|
sb.append(",\"authorize\":")
|
||||||
.append(authorize);
|
.append(authorize);
|
||||||
sb.append(",\"accessTokenUrl\":")
|
sb.append(",\"accessTokenUrl\":")
|
||||||
.append("\"").append(accessTokenUrl).append("\"");
|
.append("\"").append(accessTokenUrl).append("\"");
|
||||||
|
@ -79,6 +79,20 @@ public class WechatFilter implements Filter {
|
|||||||
// 如果header存在token则校验token,如果没有就校验session
|
// 如果header存在token则校验token,如果没有就校验session
|
||||||
String token = request.getHeader("token");
|
String token = request.getHeader("token");
|
||||||
if (!StringUtils.isEmpty(token)) {
|
if (!StringUtils.isEmpty(token)) {
|
||||||
|
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 {
|
try {
|
||||||
checkToken(token);
|
checkToken(token);
|
||||||
} catch (TokenException e) {
|
} catch (TokenException e) {
|
||||||
@ -86,9 +100,15 @@ public class WechatFilter implements Filter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信公众号自动授权
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @param response
|
||||||
|
*/
|
||||||
|
private void wechatOfficialAccountFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
|
||||||
// 判断是不是微信配置校验
|
// 判断是不是微信配置校验
|
||||||
String signatureParameter = request.getParameter("signature");
|
String signatureParameter = request.getParameter("signature");
|
||||||
String timestampParameter = request.getParameter("timestamp");
|
String timestampParameter = request.getParameter("timestamp");
|
||||||
|
@ -2,6 +2,8 @@ package com.cm.common.wechat.startup;
|
|||||||
|
|
||||||
import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
|
import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
|
||||||
import com.cm.common.wechat.manager.officialaccount.WechatOfficialAccountManager;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
@ -20,11 +22,17 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class WechatStartUp implements ApplicationRunner {
|
public class WechatStartUp implements ApplicationRunner {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(WechatStartUp.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WechatOfficialAccountProperties wechatOfficialAccountProperties;
|
private WechatOfficialAccountProperties wechatOfficialAccountProperties;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
if (wechatOfficialAccountProperties.getActivate() == null || !wechatOfficialAccountProperties.getActivate()) {
|
||||||
|
LOG.info("微信公众号未激活");
|
||||||
|
return;
|
||||||
|
}
|
||||||
// 初始化公众号AccessToken
|
// 初始化公众号AccessToken
|
||||||
WechatOfficialAccountManager wechatOfficialAccountManager = WechatOfficialAccountManager.getInstance();
|
WechatOfficialAccountManager wechatOfficialAccountManager = WechatOfficialAccountManager.getInstance();
|
||||||
wechatOfficialAccountManager.setWechatOfficialAccountProperties(wechatOfficialAccountProperties);
|
wechatOfficialAccountManager.setWechatOfficialAccountProperties(wechatOfficialAccountProperties);
|
||||||
|
@ -2,6 +2,8 @@ package com.cm.common.wechat.task;
|
|||||||
|
|
||||||
import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
|
import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
|
||||||
import com.cm.common.wechat.manager.officialaccount.WechatOfficialAccountManager;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
@ -21,11 +23,19 @@ import org.springframework.scheduling.annotation.Scheduled;
|
|||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
public class WechatTask {
|
public class WechatTask {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(WechatTask.class);
|
||||||
|
@Autowired
|
||||||
|
private WechatOfficialAccountProperties wechatOfficialAccountProperties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 刷新公众号AccessToken,每10分钟刷新一次
|
* 刷新公众号AccessToken,每10分钟刷新一次
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 0/10 * * * ?")
|
@Scheduled(cron = "0 0/10 * * * ?")
|
||||||
public void refreshOfficialAccountAccessToken() {
|
public void refreshOfficialAccountAccessToken() {
|
||||||
|
if (wechatOfficialAccountProperties.getActivate() == null || !wechatOfficialAccountProperties.getActivate()) {
|
||||||
|
LOG.info("微信公众号未激活,取消刷新");
|
||||||
|
return;
|
||||||
|
}
|
||||||
WechatOfficialAccountManager.getInstance().refreshAccessToken();
|
WechatOfficialAccountManager.getInstance().refreshAccessToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<description>验证码模块</description>
|
<description>验证码模块</description>
|
||||||
|
|
||||||
<artifactId>cloud-manager-verification-code</artifactId>
|
<artifactId>cloud-manager-sms</artifactId>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -18,6 +18,12 @@
|
|||||||
<artifactId>cloud-common</artifactId>
|
<artifactId>cloud-common</artifactId>
|
||||||
<version>1.0.1-SNAPSHOT</version>
|
<version>1.0.1-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.qcloudsms</groupId>
|
||||||
|
<artifactId>qcloudsms</artifactId>
|
||||||
|
<version>1.0.6</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import com.cm.common.constants.ISystemConstant;
|
|||||||
import com.cm.common.exception.ParamsException;
|
import com.cm.common.exception.ParamsException;
|
||||||
import com.cm.common.result.SuccessResult;
|
import com.cm.common.result.SuccessResult;
|
||||||
import com.cm.common.utils.RegexUtil;
|
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.manager.VerificationCodeManager;
|
||||||
import com.cm.manager.verificationcode.service.IVerificationCodeService;
|
import com.cm.manager.verificationcode.service.IVerificationCodeService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
@ -32,15 +33,12 @@ public class VerificationCodeController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IVerificationCodeService verificationCodeService;
|
private IVerificationCodeService verificationCodeService;
|
||||||
|
|
||||||
@GetMapping("getverificationcode/{type}/{phone}")
|
@GetMapping("getverificationcode/{phone}")
|
||||||
public SuccessResult getVerificationCode(@PathVariable("type") String type, @PathVariable("phone") String phone) {
|
public SuccessResult getVerificationCode(@PathVariable("phone") String phone) {
|
||||||
if (!StringUtils.equals(IVerificationCodeService.CUSTOM_TYPE, type)) {
|
|
||||||
throw new ParamsException("类别错误");
|
|
||||||
}
|
|
||||||
if (!RegexUtil.isPhone(phone)) {
|
if (!RegexUtil.isPhone(phone)) {
|
||||||
throw new ParamsException("手机号格式错误");
|
throw new ParamsException("手机号格式错误");
|
||||||
}
|
}
|
||||||
return verificationCodeService.getVerificationCode(type, phone);
|
return verificationCodeService.getVerificationCode(phone);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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
|
* @param phone
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
SuccessResult getVerificationCode(String type, String phone);
|
SuccessResult getVerificationCode(String phone);
|
||||||
}
|
}
|
@ -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("发送验证码失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
2
pom.xml
2
pom.xml
@ -17,7 +17,7 @@
|
|||||||
<module>cloud-common-plugin-dynamic</module>
|
<module>cloud-common-plugin-dynamic</module>
|
||||||
<module>cloud-common-plugin-dictionary</module>
|
<module>cloud-common-plugin-dictionary</module>
|
||||||
<module>cloud-common-wechat</module>
|
<module>cloud-common-wechat</module>
|
||||||
<module>cloud-manager-verification-code</module>
|
<module>cloud-manager-sms</module>
|
||||||
</modules>
|
</modules>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<description>成迈云</description>
|
<description>成迈云</description>
|
||||||
|
Loading…
Reference in New Issue
Block a user