TestController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.fjhx.controller;
  2. import cn.hutool.core.io.IoUtil;
  3. import com.fjhx.constants.WeChatConstants;
  4. import com.fjhx.utils.aes.AesException;
  5. import com.fjhx.utils.aes.WXBizMsgCrypt;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springblade.core.redis.cache.BladeRedis;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import org.w3c.dom.Document;
  14. import org.w3c.dom.Element;
  15. import org.w3c.dom.NodeList;
  16. import org.xml.sax.InputSource;
  17. import org.xml.sax.SAXException;
  18. import javax.servlet.ServletInputStream;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.xml.parsers.DocumentBuilder;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.parsers.ParserConfigurationException;
  24. import java.io.BufferedReader;
  25. import java.io.IOException;
  26. import java.io.InputStreamReader;
  27. import java.io.StringReader;
  28. @Slf4j
  29. @RestController
  30. @RequestMapping("/callback")
  31. public class TestController {
  32. @Autowired
  33. private BladeRedis bladeRedis;
  34. @RequestMapping(value = "/test")
  35. public String test() {
  36. return "hello";
  37. }
  38. /**
  39. * 验证通用开发参数及应用回调
  40. *
  41. * @link { https://developer.work.weixin.qq.com/tutorial/detail/38 }
  42. * @link { https://article.itxueyuan.com/BJ3rrG }
  43. * @link { https://developer.work.weixin.qq.com/document/10982#%E6%8E%A8%E9%80%81suite_ticket }
  44. */
  45. @GetMapping(value = "/data")
  46. public void data(HttpServletRequest request, HttpServletResponse response) {
  47. // 微信加密签名
  48. String msgSignature = request.getParameter("msg_signature");
  49. // 时间戳
  50. String timestamp = request.getParameter("timestamp");
  51. // 随机数
  52. String nonce = request.getParameter("nonce");
  53. // 随机字符串
  54. String echoStr = request.getParameter("echostr");
  55. log.error("data: msg_signature:{}; timestamp:{}; nonce:{}; echostr:{}", msgSignature, timestamp, nonce, echoStr);
  56. try {
  57. WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WeChatConstants.TOKEN, WeChatConstants.ENCODING_AES_KEY, WeChatConstants.CORP_ID);
  58. String echostr = wxBizMsgCrypt.VerifyURL(msgSignature, timestamp, nonce, echoStr);
  59. log.error("dataEchostr: " + echostr);
  60. // 验证URL成功,将sEchoStr返回
  61. response.getWriter().print(echostr);
  62. } catch (Exception e) {
  63. //验证URL失败,错误原因请查看异常
  64. log.error("验证失败", e);
  65. }
  66. }
  67. /**
  68. * 验证通用开发参数及应用回调
  69. *
  70. * @link { https://developer.work.weixin.qq.com/tutorial/detail/38 }
  71. */
  72. @PostMapping(value = "/data")
  73. public String data(HttpServletRequest request) {
  74. // 微信加密签名
  75. String sReqMsgSig = request.getParameter("msg_signature");
  76. // 时间戳
  77. String sReqTimeStamp = request.getParameter("timestamp");
  78. // 随机数
  79. String sReqNonce = request.getParameter("nonce");
  80. // 密文,对应POST请求的数据
  81. StringBuilder sReqData = new StringBuilder();
  82. BufferedReader reader = null;
  83. try {
  84. ServletInputStream in = request.getInputStream();
  85. reader = new BufferedReader(new InputStreamReader(in));
  86. String tempStr;
  87. while (null != (tempStr = reader.readLine())) {
  88. sReqData.append(tempStr);
  89. }
  90. WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WeChatConstants.TOKEN, WeChatConstants.ENCODING_AES_KEY, WeChatConstants.CORP_ID);
  91. // 刷新ticket,AuthCode
  92. String sMsg = wxBizMsgCrypt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData.toString());
  93. log.error("command:" + sMsg);
  94. // TODO: 解析出明文xml标签的内容进行处理
  95. // For example:
  96. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  97. DocumentBuilder db = dbf.newDocumentBuilder();
  98. StringReader sr = new StringReader(sMsg);
  99. InputSource is = new InputSource(sr);
  100. Document document = db.parse(is);
  101. Element root = document.getDocumentElement();
  102. NodeList nodelist = root.getElementsByTagName("Content");
  103. for (int i = 0; i < nodelist.getLength(); i++) {
  104. String Content = nodelist.item(i).getTextContent();
  105. System.out.println("Content" + i + ": " + Content);
  106. }
  107. } catch (IOException | SAXException | ParserConfigurationException | AesException e) {
  108. e.printStackTrace();
  109. return "error";
  110. } finally {
  111. IoUtil.close(reader);
  112. }
  113. return "success";
  114. }
  115. }