mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-23 23:58:44 +08:00
demo 增加对aes消息加密的支持
This commit is contained in:
parent
c13b8ac485
commit
42ce9a1918
@ -20,7 +20,7 @@
|
||||
<slf4j.version>1.7.2</slf4j.version>
|
||||
<aspectj.version>1.8.9</aspectj.version>
|
||||
<fastjson.version>1.2.6</fastjson.version>
|
||||
<commons-lang3.version>3.1</commons-lang3.version>
|
||||
<commons-lang3.version>3.4</commons-lang3.version>
|
||||
<jetty-maven-plugin.version>9.3.10.v20160621</jetty-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
@ -12,6 +12,8 @@ public abstract class WxConfig {
|
||||
|
||||
public abstract String getAppsecret();
|
||||
|
||||
public abstract String getAesKey();
|
||||
|
||||
public abstract WxAccountEnum getWxAccountEnum();
|
||||
|
||||
public int getPubId() {
|
||||
|
@ -19,6 +19,9 @@ public class WxGzh1Config extends WxConfig {
|
||||
@Value("#{gzh1WxProperties.wx_appsecret}")
|
||||
private String appsecret;
|
||||
|
||||
@Value("#{gzh1WxProperties.wx_aeskey}")
|
||||
private String aesKey;
|
||||
|
||||
@Override
|
||||
public String getToken() {
|
||||
return this.token;
|
||||
@ -34,6 +37,11 @@ public class WxGzh1Config extends WxConfig {
|
||||
return this.appsecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAesKey() {
|
||||
return this.aesKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxAccountEnum getWxAccountEnum() {
|
||||
return WxAccountEnum.GZH1;
|
||||
|
@ -19,6 +19,9 @@ public class WxGzh2Config extends WxConfig {
|
||||
@Value("#{gzh2WxProperties.wx_appsecret}")
|
||||
private String appsecret;
|
||||
|
||||
@Value("#{gzh2WxProperties.wx_aeskey}")
|
||||
private String aesKey;
|
||||
|
||||
@Override
|
||||
public String getToken() {
|
||||
return this.token;
|
||||
@ -34,6 +37,11 @@ public class WxGzh2Config extends WxConfig {
|
||||
return this.appsecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAesKey() {
|
||||
return this.aesKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxAccountEnum getWxAccountEnum() {
|
||||
return WxAccountEnum.GZH2;
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.github.binarywang.demo.spring.controller;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@ -27,7 +28,8 @@ public abstract class AbstractWxPortalController {
|
||||
@RequestParam("timestamp") String timestamp,
|
||||
@RequestParam("nonce") String nonce,
|
||||
@RequestParam("echostr") String echostr) {
|
||||
this.logger.info("接收到来自微信服务器的认证消息");
|
||||
this.logger.info("\n接收到来自微信服务器的认证消息:[{},{},{},{}]",
|
||||
signature, timestamp, nonce, echostr);
|
||||
|
||||
if (this.getWxService().checkSignature(timestamp, nonce, signature)) {
|
||||
return echostr;
|
||||
@ -37,24 +39,40 @@ public abstract class AbstractWxPortalController {
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, produces = "application/xml; charset=UTF-8")
|
||||
public @ResponseBody String post(@RequestBody String requestBody) {
|
||||
public @ResponseBody String post(@RequestBody String requestBody,
|
||||
@RequestParam("signature") String signature,
|
||||
@RequestParam("encrypt_type") String encType,
|
||||
@RequestParam("msg_signature") String msgSignature,
|
||||
@RequestParam("timestamp") String timestamp,
|
||||
@RequestParam("nonce") String nonce) {
|
||||
this.logger.info("\n接收微信请求:[{},{},{},{},{}]\n{} ",
|
||||
signature, encType, msgSignature, timestamp, nonce, requestBody);
|
||||
|
||||
this.logger.debug("\n接收微信请求:{} ", requestBody);
|
||||
String out = null;
|
||||
if (encType == null) {
|
||||
// 明文传输的消息
|
||||
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
|
||||
WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
|
||||
if (outMessage == null) {
|
||||
return "";
|
||||
}
|
||||
out = outMessage.toXml();
|
||||
}else if ("aes".equals(encType)) {
|
||||
// aes加密的消息
|
||||
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody,
|
||||
this.getWxService().getWxMpConfigStorage(), timestamp, nonce, msgSignature);
|
||||
this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
|
||||
WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
|
||||
if (outMessage == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
BaseWxService wxService = this.getWxService();
|
||||
|
||||
WxMpXmlOutMessage out = wxService
|
||||
.route(WxMpXmlMessage.fromXml(requestBody));
|
||||
|
||||
if (out == null) {
|
||||
return "";
|
||||
out = outMessage.toEncryptedXml(this.getWxService().getWxMpConfigStorage());
|
||||
}
|
||||
|
||||
String outXml = out.toXml();
|
||||
this.logger.debug("\n组装回复信息:{}", out);
|
||||
|
||||
this.logger.debug("\n组装回复信息:{}", outXml);
|
||||
|
||||
return outXml;
|
||||
return out;
|
||||
}
|
||||
|
||||
protected abstract BaseWxService getWxService();
|
||||
|
@ -63,6 +63,7 @@ public abstract class BaseWxService extends WxMpServiceImpl {
|
||||
config.setAppId(this.getServerConfig().getAppid());// 设置微信公众号的appid
|
||||
config.setSecret(this.getServerConfig().getAppsecret());// 设置微信公众号的app corpSecret
|
||||
config.setToken(this.getServerConfig().getToken());// 设置微信公众号的token
|
||||
config.setAesKey(this.getServerConfig().getAesKey());// 设置消息加解密密钥
|
||||
super.setWxMpConfigStorage(config);
|
||||
|
||||
this.refreshRouter();
|
||||
|
@ -1,3 +1,4 @@
|
||||
wx_appid=
|
||||
wx_appsecret=
|
||||
wx_token=
|
||||
wx_aeskey=
|
||||
|
@ -1,3 +1,4 @@
|
||||
wx_appid=
|
||||
wx_appsecret=
|
||||
wx_token=
|
||||
wx_aeskey=
|
||||
|
Loading…
Reference in New Issue
Block a user