@@ -20,7 +20,7 @@ | |||||
<slf4j.version>1.7.2</slf4j.version> | <slf4j.version>1.7.2</slf4j.version> | ||||
<aspectj.version>1.8.9</aspectj.version> | <aspectj.version>1.8.9</aspectj.version> | ||||
<fastjson.version>1.2.6</fastjson.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> | <jetty-maven-plugin.version>9.3.10.v20160621</jetty-maven-plugin.version> | ||||
</properties> | </properties> | ||||
@@ -12,6 +12,8 @@ public abstract class WxConfig { | |||||
public abstract String getAppsecret(); | public abstract String getAppsecret(); | ||||
public abstract String getAesKey(); | |||||
public abstract WxAccountEnum getWxAccountEnum(); | public abstract WxAccountEnum getWxAccountEnum(); | ||||
public int getPubId() { | public int getPubId() { | ||||
@@ -19,6 +19,9 @@ public class WxGzh1Config extends WxConfig { | |||||
@Value("#{gzh1WxProperties.wx_appsecret}") | @Value("#{gzh1WxProperties.wx_appsecret}") | ||||
private String appsecret; | private String appsecret; | ||||
@Value("#{gzh1WxProperties.wx_aeskey}") | |||||
private String aesKey; | |||||
@Override | @Override | ||||
public String getToken() { | public String getToken() { | ||||
return this.token; | return this.token; | ||||
@@ -34,6 +37,11 @@ public class WxGzh1Config extends WxConfig { | |||||
return this.appsecret; | return this.appsecret; | ||||
} | } | ||||
@Override | |||||
public String getAesKey() { | |||||
return this.aesKey; | |||||
} | |||||
@Override | @Override | ||||
public WxAccountEnum getWxAccountEnum() { | public WxAccountEnum getWxAccountEnum() { | ||||
return WxAccountEnum.GZH1; | return WxAccountEnum.GZH1; | ||||
@@ -19,6 +19,9 @@ public class WxGzh2Config extends WxConfig { | |||||
@Value("#{gzh2WxProperties.wx_appsecret}") | @Value("#{gzh2WxProperties.wx_appsecret}") | ||||
private String appsecret; | private String appsecret; | ||||
@Value("#{gzh2WxProperties.wx_aeskey}") | |||||
private String aesKey; | |||||
@Override | @Override | ||||
public String getToken() { | public String getToken() { | ||||
return this.token; | return this.token; | ||||
@@ -34,6 +37,11 @@ public class WxGzh2Config extends WxConfig { | |||||
return this.appsecret; | return this.appsecret; | ||||
} | } | ||||
@Override | |||||
public String getAesKey() { | |||||
return this.aesKey; | |||||
} | |||||
@Override | @Override | ||||
public WxAccountEnum getWxAccountEnum() { | public WxAccountEnum getWxAccountEnum() { | ||||
return WxAccountEnum.GZH2; | return WxAccountEnum.GZH2; | ||||
@@ -1,5 +1,6 @@ | |||||
package com.github.binarywang.demo.spring.controller; | package com.github.binarywang.demo.spring.controller; | ||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
import org.springframework.web.bind.annotation.RequestBody; | import org.springframework.web.bind.annotation.RequestBody; | ||||
@@ -27,7 +28,8 @@ public abstract class AbstractWxPortalController { | |||||
@RequestParam("timestamp") String timestamp, | @RequestParam("timestamp") String timestamp, | ||||
@RequestParam("nonce") String nonce, | @RequestParam("nonce") String nonce, | ||||
@RequestParam("echostr") String echostr) { | @RequestParam("echostr") String echostr) { | ||||
this.logger.info("接收到来自微信服务器的认证消息"); | |||||
this.logger.info("\n接收到来自微信服务器的认证消息:[{},{},{},{}]", | |||||
signature, timestamp, nonce, echostr); | |||||
if (this.getWxService().checkSignature(timestamp, nonce, signature)) { | if (this.getWxService().checkSignature(timestamp, nonce, signature)) { | ||||
return echostr; | return echostr; | ||||
@@ -37,24 +39,40 @@ public abstract class AbstractWxPortalController { | |||||
} | } | ||||
@RequestMapping(method = RequestMethod.POST, produces = "application/xml; charset=UTF-8") | @RequestMapping(method = RequestMethod.POST, produces = "application/xml; charset=UTF-8") | ||||
public @ResponseBody String post(@RequestBody String requestBody) { | |||||
this.logger.debug("\n接收微信请求:{} ", requestBody); | |||||
BaseWxService wxService = this.getWxService(); | |||||
WxMpXmlOutMessage out = wxService | |||||
.route(WxMpXmlMessage.fromXml(requestBody)); | |||||
if (out == null) { | |||||
return ""; | |||||
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); | |||||
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 ""; | |||||
} | |||||
out = outMessage.toEncryptedXml(this.getWxService().getWxMpConfigStorage()); | |||||
} | } | ||||
String outXml = out.toXml(); | |||||
this.logger.debug("\n组装回复信息:{}", outXml); | |||||
this.logger.debug("\n组装回复信息:{}", out); | |||||
return outXml; | |||||
return out; | |||||
} | } | ||||
protected abstract BaseWxService getWxService(); | protected abstract BaseWxService getWxService(); | ||||
@@ -63,6 +63,7 @@ public abstract class BaseWxService extends WxMpServiceImpl { | |||||
config.setAppId(this.getServerConfig().getAppid());// 设置微信公众号的appid | config.setAppId(this.getServerConfig().getAppid());// 设置微信公众号的appid | ||||
config.setSecret(this.getServerConfig().getAppsecret());// 设置微信公众号的app corpSecret | config.setSecret(this.getServerConfig().getAppsecret());// 设置微信公众号的app corpSecret | ||||
config.setToken(this.getServerConfig().getToken());// 设置微信公众号的token | config.setToken(this.getServerConfig().getToken());// 设置微信公众号的token | ||||
config.setAesKey(this.getServerConfig().getAesKey());// 设置消息加解密密钥 | |||||
super.setWxMpConfigStorage(config); | super.setWxMpConfigStorage(config); | ||||
this.refreshRouter(); | this.refreshRouter(); | ||||
@@ -1,3 +1,4 @@ | |||||
wx_appid= | wx_appid= | ||||
wx_appsecret= | wx_appsecret= | ||||
wx_token= | wx_token= | ||||
wx_aeskey= |
@@ -1,3 +1,4 @@ | |||||
wx_appid= | wx_appid= | ||||
wx_appsecret= | wx_appsecret= | ||||
wx_token= | wx_token= | ||||
wx_aeskey= |