From 42ce9a19189a511c8076f786ee514513b1e368d9 Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Wed, 13 Jul 2016 16:05:04 +0800 Subject: [PATCH] =?UTF-8?q?demo=20=E5=A2=9E=E5=8A=A0=E5=AF=B9aes=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=8A=A0=E5=AF=86=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weixin-java-demo-with-spring/pom.xml | 2 +- .../demo/spring/config/WxConfig.java | 2 + .../demo/spring/config/WxGzh1Config.java | 8 ++++ .../demo/spring/config/WxGzh2Config.java | 8 ++++ .../AbstractWxPortalController.java | 46 +++++++++++++------ .../demo/spring/service/BaseWxService.java | 1 + .../resources/wx-gzh1.properties.template | 1 + .../resources/wx-gzh2.properties.template | 1 + 8 files changed, 54 insertions(+), 15 deletions(-) diff --git a/weixin-java-demo-with-spring/pom.xml b/weixin-java-demo-with-spring/pom.xml index 469ac9e22..517846861 100644 --- a/weixin-java-demo-with-spring/pom.xml +++ b/weixin-java-demo-with-spring/pom.xml @@ -20,7 +20,7 @@ 1.7.2 1.8.9 1.2.6 - 3.1 + 3.4 9.3.10.v20160621 diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java index a65d4000e..782f24051 100644 --- a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java @@ -12,6 +12,8 @@ public abstract class WxConfig { public abstract String getAppsecret(); + public abstract String getAesKey(); + public abstract WxAccountEnum getWxAccountEnum(); public int getPubId() { diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java index fee34f431..c1cfc01c7 100644 --- a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java @@ -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; diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java index d284630a8..8c593909c 100644 --- a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java @@ -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; diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java index 8bae46695..dba3f8406 100644 --- a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java @@ -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(); diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java index 133c7445b..ac61bea0b 100644 --- a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java @@ -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(); diff --git a/weixin-java-demo-with-spring/src/main/resources/wx-gzh1.properties.template b/weixin-java-demo-with-spring/src/main/resources/wx-gzh1.properties.template index 76afcd9ea..302903e55 100644 --- a/weixin-java-demo-with-spring/src/main/resources/wx-gzh1.properties.template +++ b/weixin-java-demo-with-spring/src/main/resources/wx-gzh1.properties.template @@ -1,3 +1,4 @@ wx_appid= wx_appsecret= wx_token= +wx_aeskey= diff --git a/weixin-java-demo-with-spring/src/main/resources/wx-gzh2.properties.template b/weixin-java-demo-with-spring/src/main/resources/wx-gzh2.properties.template index 76afcd9ea..302903e55 100644 --- a/weixin-java-demo-with-spring/src/main/resources/wx-gzh2.properties.template +++ b/weixin-java-demo-with-spring/src/main/resources/wx-gzh2.properties.template @@ -1,3 +1,4 @@ wx_appid= wx_appsecret= wx_token= +wx_aeskey=