Commit bf0c628f authored by shiyu's avatar shiyu

公众号

parent 7da05a0f
......@@ -2,11 +2,13 @@ package com.wwdz.ch.wx.api;
import com.alibaba.fastjson.JSONObject;
import com.wwdz.ch.wx.api.aes.AesException;
import com.wwdz.ch.wx.api.aes.WXBizMsgCrypt;
import com.wwdz.ch.wx.util.StringUtil;
import com.xxdxxs.utils.JsonUtils;
import com.xxdxxs.utils.StringUtils;
import com.xxdxxs.utils.XmlUtils;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -62,7 +64,6 @@ public class OfficialAccountController {
InputStream input = null;
try {
logger.info("接收公众号推送信息校验 -> appid: {}, signature:{}, timestamp:{}, nonce:{}, echostr : {}", APPID, signature, timestamp, nonce, echostr);
WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(OFFICIALACCOUNTTOKEN, OFFICIALACCOUNTKEY, APPID);
output = new ByteArrayOutputStream();
input = request.getInputStream();
byte[] by = new byte[1024];
......@@ -72,8 +73,8 @@ public class OfficialAccountController {
}
String xmlInfo = new String(output.toByteArray(), "UTF-8");
logger.info("微信通知回调接口原文:{}", xmlInfo);
String xmlStr = wxBizMsgCrypt.decryptMsg(signature, timestamp, nonce, xmlInfo);
logger.info("解密后获取公众号推送的信息内容 : {}", xmlStr);
WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(OFFICIALACCOUNTTOKEN, OFFICIALACCOUNTKEY, APPID);
String xmlStr = wxBizMsgCrypt.decryptXmlMsg(signature, timestamp, nonce, xmlInfo);
List<Map<String, String>> list = XmlUtils.fromXml(xmlStr, "xml");
Map<String, String> contentMap = list.get(0);
String event = contentMap.get("Event");
......@@ -105,6 +106,29 @@ public class OfficialAccountController {
}
public static void main(String[] args) throws AesException, DocumentException {
String xml = "<xml><ToUserName><![CDATA[gh_075dfbc935ef]]></ToUserName>\n" +
"<FromUserName><![CDATA[oNPTN6QaVliZfKB53OzZRPBZr_r8]]></FromUserName>\n" +
"<CreateTime>1697165124</CreateTime>\n" +
"<MsgType><![CDATA[event]]></MsgType>\n" +
"<Event><![CDATA[subscribe]]></Event>\n" +
"<EventKey><![CDATA[]]></EventKey>\n" +
"</xml>";
WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt("Uu6GHYOiP1xJ6GaBMyXfnITufAGJTe3Q", "noeN1amYRvzrVmE2GnC5VnsIfgPEmv6bhX54wJOARlM", "wx7db8eb146a194083");
List<Map<String, String>> list = XmlUtils.fromXml(xml, "xml");
Map<String, String> contentMap = list.get(0);
System.out.println(contentMap.get("Event") + ">>" + contentMap.get("FromUserName") + ">>" + contentMap.get("CreateTime"));
String event = wxBizMsgCrypt.decrypt(contentMap.get("Event"));
if (SUBSCRIBE_EVENT.equals(event) || UNSUBSCRIBE_EVENT.equals(event)) {
String userOpenId = wxBizMsgCrypt.decrypt(contentMap.get("FromUserName"));
String createTime = wxBizMsgCrypt.decrypt(contentMap.get("CreateTime"));
Date date = new Date(createTime);
System.out.println(userOpenId + ">>" + event + ">>" + date);
}
}
@RequestMapping(value ="/getSubscribeNotice", consumes = "text/xml")
public Object getSubscribeNotice(@RequestParam(required = false, name = "signature") String signature,
@RequestParam(required = false, name = "timestamp") String timestamp,
......
......@@ -15,13 +15,17 @@ package com.wwdz.ch.wx.api.aes;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxdxxs.utils.XmlUtils;
import org.apache.commons.codec.binary.Base64;
import org.dom4j.DocumentException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
......@@ -267,6 +271,15 @@ public class WXBizMsgCrypt {
}
public String decryptXmlMsg(String msgSignature, String timeStamp, String nonce, String postData)
throws AesException, DocumentException {
Object[] encrypt = XMLParse.extract(postData);
String result = decrypt(encrypt[1].toString());
return result;
}
public String decryptJsonMsg(String msgSignature, String timeStamp, String nonce, String postData)
throws AesException {
JSONObject jsonObject = JSON.parseObject(postData);
......
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