Skip to content

Commit 42ce9a1

Browse files
committed
demo 增加对aes消息加密的支持
1 parent c13b8ac commit 42ce9a1

File tree

8 files changed

+56
-17
lines changed

8 files changed

+56
-17
lines changed

weixin-java-demo-with-spring/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<slf4j.version>1.7.2</slf4j.version>
2121
<aspectj.version>1.8.9</aspectj.version>
2222
<fastjson.version>1.2.6</fastjson.version>
23-
<commons-lang3.version>3.1</commons-lang3.version>
23+
<commons-lang3.version>3.4</commons-lang3.version>
2424
<jetty-maven-plugin.version>9.3.10.v20160621</jetty-maven-plugin.version>
2525
</properties>
2626

weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public abstract class WxConfig {
1212

1313
public abstract String getAppsecret();
1414

15+
public abstract String getAesKey();
16+
1517
public abstract WxAccountEnum getWxAccountEnum();
1618

1719
public int getPubId() {

weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class WxGzh1Config extends WxConfig {
1919
@Value("#{gzh1WxProperties.wx_appsecret}")
2020
private String appsecret;
2121

22+
@Value("#{gzh1WxProperties.wx_aeskey}")
23+
private String aesKey;
24+
2225
@Override
2326
public String getToken() {
2427
return this.token;
@@ -34,6 +37,11 @@ public String getAppsecret() {
3437
return this.appsecret;
3538
}
3639

40+
@Override
41+
public String getAesKey() {
42+
return this.aesKey;
43+
}
44+
3745
@Override
3846
public WxAccountEnum getWxAccountEnum() {
3947
return WxAccountEnum.GZH1;

weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class WxGzh2Config extends WxConfig {
1919
@Value("#{gzh2WxProperties.wx_appsecret}")
2020
private String appsecret;
2121

22+
@Value("#{gzh2WxProperties.wx_aeskey}")
23+
private String aesKey;
24+
2225
@Override
2326
public String getToken() {
2427
return this.token;
@@ -34,6 +37,11 @@ public String getAppsecret() {
3437
return this.appsecret;
3538
}
3639

40+
@Override
41+
public String getAesKey() {
42+
return this.aesKey;
43+
}
44+
3745
@Override
3846
public WxAccountEnum getWxAccountEnum() {
3947
return WxAccountEnum.GZH2;

weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.demo.spring.controller;
22

3+
import org.apache.commons.lang3.StringUtils;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56
import org.springframework.web.bind.annotation.RequestBody;
@@ -27,7 +28,8 @@ public abstract class AbstractWxPortalController {
2728
@RequestParam("timestamp") String timestamp,
2829
@RequestParam("nonce") String nonce,
2930
@RequestParam("echostr") String echostr) {
30-
this.logger.info("接收到来自微信服务器的认证消息");
31+
this.logger.info("\n接收到来自微信服务器的认证消息:[{},{},{},{}]",
32+
signature, timestamp, nonce, echostr);
3133

3234
if (this.getWxService().checkSignature(timestamp, nonce, signature)) {
3335
return echostr;
@@ -37,24 +39,40 @@ public abstract class AbstractWxPortalController {
3739
}
3840

3941
@RequestMapping(method = RequestMethod.POST, produces = "application/xml; charset=UTF-8")
40-
public @ResponseBody String post(@RequestBody String requestBody) {
41-
42-
this.logger.debug("\n接收微信请求:{} ", requestBody);
43-
44-
BaseWxService wxService = this.getWxService();
45-
46-
WxMpXmlOutMessage out = wxService
47-
.route(WxMpXmlMessage.fromXml(requestBody));
48-
49-
if (out == null) {
50-
return "";
42+
public @ResponseBody String post(@RequestBody String requestBody,
43+
@RequestParam("signature") String signature,
44+
@RequestParam("encrypt_type") String encType,
45+
@RequestParam("msg_signature") String msgSignature,
46+
@RequestParam("timestamp") String timestamp,
47+
@RequestParam("nonce") String nonce) {
48+
this.logger.info("\n接收微信请求:[{},{},{},{},{}]\n{} ",
49+
signature, encType, msgSignature, timestamp, nonce, requestBody);
50+
51+
String out = null;
52+
if (encType == null) {
53+
// 明文传输的消息
54+
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
55+
WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
56+
if (outMessage == null) {
57+
return "";
58+
}
59+
out = outMessage.toXml();
60+
}else if ("aes".equals(encType)) {
61+
// aes加密的消息
62+
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody,
63+
this.getWxService().getWxMpConfigStorage(), timestamp, nonce, msgSignature);
64+
this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
65+
WxMpXmlOutMessage outMessage = this.getWxService().route(inMessage);
66+
if (outMessage == null) {
67+
return "";
68+
}
69+
70+
out = outMessage.toEncryptedXml(this.getWxService().getWxMpConfigStorage());
5171
}
5272

53-
String outXml = out.toXml();
54-
55-
this.logger.debug("\n组装回复信息:{}", outXml);
73+
this.logger.debug("\n组装回复信息:{}", out);
5674

57-
return outXml;
75+
return out;
5876
}
5977

6078
protected abstract BaseWxService getWxService();

weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void init() {
6363
config.setAppId(this.getServerConfig().getAppid());// 设置微信公众号的appid
6464
config.setSecret(this.getServerConfig().getAppsecret());// 设置微信公众号的app corpSecret
6565
config.setToken(this.getServerConfig().getToken());// 设置微信公众号的token
66+
config.setAesKey(this.getServerConfig().getAesKey());// 设置消息加解密密钥
6667
super.setWxMpConfigStorage(config);
6768

6869
this.refreshRouter();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
wx_appid=
22
wx_appsecret=
33
wx_token=
4+
wx_aeskey=
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
wx_appid=
22
wx_appsecret=
33
wx_token=
4+
wx_aeskey=

0 commit comments

Comments
 (0)