Commit 7da05a0f authored by shiyu's avatar shiyu

公众号

parent ca818d19
...@@ -82,6 +82,11 @@ ...@@ -82,6 +82,11 @@
<version>1.18.24</version> <version>1.18.24</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.6</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -50,11 +50,19 @@ public class OfficialAccountController { ...@@ -50,11 +50,19 @@ public class OfficialAccountController {
@RequestMapping(value ="/getNotice") @RequestMapping(value ="/getNotice")
public String getNotice(HttpServletRequest request, HttpServletResponse response){ public Object getNotice(@RequestParam(required = false, name = "signature") String signature,
logger.info("1111111111111111"); @RequestParam(required = false, name = "timestamp") String timestamp,
@RequestParam(required = false, name = "nonce") String nonce,
@RequestParam(required = false, name = "echostr") String echostr,
HttpServletRequest request, HttpServletResponse response){
if (StringUtils.hasLength(echostr)) {
return Long.valueOf(echostr.trim());
}
ByteArrayOutputStream output = null; ByteArrayOutputStream output = null;
InputStream input = null; InputStream input = null;
try { try {
logger.info("接收公众号推送信息校验 -> appid: {}, signature:{}, timestamp:{}, nonce:{}, echostr : {}", APPID, signature, timestamp, nonce, echostr);
WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(OFFICIALACCOUNTTOKEN, OFFICIALACCOUNTKEY, APPID);
output = new ByteArrayOutputStream(); output = new ByteArrayOutputStream();
input = request.getInputStream(); input = request.getInputStream();
byte[] by = new byte[1024]; byte[] by = new byte[1024];
...@@ -64,10 +72,10 @@ public class OfficialAccountController { ...@@ -64,10 +72,10 @@ public class OfficialAccountController {
} }
String xmlInfo = new String(output.toByteArray(), "UTF-8"); String xmlInfo = new String(output.toByteArray(), "UTF-8");
logger.info("微信通知回调接口原文:{}", xmlInfo); logger.info("微信通知回调接口原文:{}", xmlInfo);
String xmlStr = wxBizMsgCrypt.decryptMsg(signature, timestamp, nonce, xmlInfo);
List<Map<String, String>> list = XmlUtils.fromXml(xmlInfo, "xml"); logger.info("解密后获取公众号推送的信息内容 : {}", xmlStr);
List<Map<String, String>> list = XmlUtils.fromXml(xmlStr, "xml");
Map<String, String> contentMap = list.get(0); Map<String, String> contentMap = list.get(0);
String event = contentMap.get("Event"); String event = contentMap.get("Event");
if (SUBSCRIBE_EVENT.equals(event) || UNSUBSCRIBE_EVENT.equals(event)) { if (SUBSCRIBE_EVENT.equals(event) || UNSUBSCRIBE_EVENT.equals(event)) {
String userOpenId = contentMap.get("FromUserName"); String userOpenId = contentMap.get("FromUserName");
...@@ -76,7 +84,7 @@ public class OfficialAccountController { ...@@ -76,7 +84,7 @@ public class OfficialAccountController {
logger.info("openId:{}, event :{}, time:{}", userOpenId, event, date); logger.info("openId:{}, event :{}, time:{}", userOpenId, event, date);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("微信通知回调接口 error : {}", e); logger.error("微信公众号通知回调接口 error : {}", e);
} finally { } finally {
if (output != null) { if (output != null) {
try { try {
...@@ -97,7 +105,7 @@ public class OfficialAccountController { ...@@ -97,7 +105,7 @@ public class OfficialAccountController {
} }
@RequestMapping(value ="/getSubscribeNotice") @RequestMapping(value ="/getSubscribeNotice", consumes = "text/xml")
public Object getSubscribeNotice(@RequestParam(required = false, name = "signature") String signature, public Object getSubscribeNotice(@RequestParam(required = false, name = "signature") String signature,
@RequestParam(required = false, name = "timestamp") String timestamp, @RequestParam(required = false, name = "timestamp") String timestamp,
@RequestParam(required = false, name = "nonce") String nonce, @RequestParam(required = false, name = "nonce") String nonce,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment