|
@@ -1,8 +1,9 @@
|
|
|
package com.fjhx.controller;
|
|
|
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
import com.fjhx.constants.WeChatConstants;
|
|
|
+import com.fjhx.utils.aes.AesException;
|
|
|
import com.fjhx.utils.aes.WXBizMsgCrypt;
|
|
|
-import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
@@ -12,13 +13,16 @@ import org.w3c.dom.Document;
|
|
|
import org.w3c.dom.Element;
|
|
|
import org.w3c.dom.NodeList;
|
|
|
import org.xml.sax.InputSource;
|
|
|
+import org.xml.sax.SAXException;
|
|
|
|
|
|
import javax.servlet.ServletInputStream;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
+import javax.xml.parsers.ParserConfigurationException;
|
|
|
import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.io.StringReader;
|
|
|
|
|
@@ -52,7 +56,7 @@ public class TestController {
|
|
|
// 随机字符串
|
|
|
String echoStr = request.getParameter("echostr");
|
|
|
|
|
|
- log.info("data: msg_signature:{}; timestamp:{}; nonce:{}; echostr:{}", msgSignature, timestamp, nonce, echoStr);
|
|
|
+ log.error("data: msg_signature:{}; timestamp:{}; nonce:{}; echostr:{}", msgSignature, timestamp, nonce, echoStr);
|
|
|
|
|
|
try {
|
|
|
WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WeChatConstants.TOKEN, WeChatConstants.ENCODING_AES_KEY, WeChatConstants.CORP_ID);
|
|
@@ -72,9 +76,8 @@ public class TestController {
|
|
|
*
|
|
|
* @link { https://developer.work.weixin.qq.com/tutorial/detail/38 }
|
|
|
*/
|
|
|
- @SneakyThrows
|
|
|
@PostMapping(value = "/command")
|
|
|
- public String command(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ public String command(HttpServletRequest request) {
|
|
|
// 微信加密签名
|
|
|
String sReqMsgSig = request.getParameter("msg_signature");
|
|
|
// 时间戳
|
|
@@ -84,32 +87,44 @@ public class TestController {
|
|
|
|
|
|
// 密文,对应POST请求的数据
|
|
|
StringBuilder sReqData = new StringBuilder();
|
|
|
- ServletInputStream in = request.getInputStream();
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
|
- String tempStr;
|
|
|
- while (null != (tempStr = reader.readLine())) {
|
|
|
- sReqData.append(tempStr);
|
|
|
- }
|
|
|
|
|
|
- WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WeChatConstants.TOKEN, WeChatConstants.ENCODING_AES_KEY, WeChatConstants.CORP_ID);
|
|
|
-
|
|
|
- // 刷新ticket,AuthCode
|
|
|
- String sMsg = wxBizMsgCrypt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData.toString());
|
|
|
- log.error("command:" + sMsg);
|
|
|
- // TODO: 解析出明文xml标签的内容进行处理
|
|
|
- // For example:
|
|
|
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
|
- DocumentBuilder db = dbf.newDocumentBuilder();
|
|
|
- StringReader sr = new StringReader(sMsg);
|
|
|
- InputSource is = new InputSource(sr);
|
|
|
- Document document = db.parse(is);
|
|
|
-
|
|
|
- Element root = document.getDocumentElement();
|
|
|
- NodeList nodelist = root.getElementsByTagName("Content");
|
|
|
-
|
|
|
- for (int i = 0; i < nodelist.getLength(); i++) {
|
|
|
- String Content = nodelist.item(i).getTextContent();
|
|
|
- System.out.println("Content" + i + ": " + Content);
|
|
|
+ BufferedReader reader = null;
|
|
|
+ try {
|
|
|
+ ServletInputStream in = request.getInputStream();
|
|
|
+ reader = new BufferedReader(new InputStreamReader(in));
|
|
|
+
|
|
|
+ String tempStr;
|
|
|
+ while (null != (tempStr = reader.readLine())) {
|
|
|
+ sReqData.append(tempStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WeChatConstants.TOKEN, WeChatConstants.ENCODING_AES_KEY, WeChatConstants.CORP_ID);
|
|
|
+
|
|
|
+ // 刷新ticket,AuthCode
|
|
|
+ String sMsg = wxBizMsgCrypt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData.toString());
|
|
|
+
|
|
|
+ log.error("command:" + sMsg);
|
|
|
+
|
|
|
+ // TODO: 解析出明文xml标签的内容进行处理
|
|
|
+ // For example:
|
|
|
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
|
+ DocumentBuilder db = dbf.newDocumentBuilder();
|
|
|
+ StringReader sr = new StringReader(sMsg);
|
|
|
+ InputSource is = new InputSource(sr);
|
|
|
+ Document document = db.parse(is);
|
|
|
+
|
|
|
+ Element root = document.getDocumentElement();
|
|
|
+ NodeList nodelist = root.getElementsByTagName("Content");
|
|
|
+
|
|
|
+ for (int i = 0; i < nodelist.getLength(); i++) {
|
|
|
+ String Content = nodelist.item(i).getTextContent();
|
|
|
+ System.out.println("Content" + i + ": " + Content);
|
|
|
+ }
|
|
|
+ } catch (IOException | SAXException | ParserConfigurationException | AesException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "error";
|
|
|
+ } finally {
|
|
|
+ IoUtil.close(reader);
|
|
|
}
|
|
|
|
|
|
return "success";
|