WxPushReceptionController.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.cybertogether.wx.notice.web;
  2. import com.cybertogether.wx.notice.infrastructure.Downstream;
  3. import com.cybertogether.wx.notice.infrastructure.HttpClientUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.*;
  8. /**
  9. * 接收微信推送通知
  10. * 授权流程文档
  11. * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=
  12. */
  13. @RestController
  14. @RequestMapping("/wx/pushReception")
  15. public class WxPushReceptionController {
  16. private static final Logger LOGGER = LoggerFactory.getLogger(WxPushReceptionController.class);
  17. @Autowired
  18. Downstream downstream;
  19. /**
  20. * 接收微信的授权事件通知
  21. * 开发配置-开发资料-授权事件接收配置
  22. * localhost:8080/pandora/wx/pushReception/authorization?timestamp=34353535&nonce=4342344&msg_signature=ee661ba5dbbf82d488aaf18883808411d9c73b31
  23. * todo 应该在此处进行解密并验签,然后将解密后的数据体进行广播转发
  24. */
  25. @PostMapping("/authorization")
  26. public String receptionAuthorizationEvent(@RequestParam("timestamp") String timestamp,
  27. @RequestParam("nonce") String nonce,
  28. @RequestParam("msg_signature") String msgSignature,
  29. @RequestBody String encryptedXmlBodyText) {
  30. LOGGER.info("微信通知转发服务-收到微信授权通知");
  31. String urlParams = String.format("?timestamp=%s&nonce=%s&msg_signature=%s", timestamp, nonce, msgSignature);
  32. for (String url : downstream.getUrls()) {
  33. try {
  34. HttpClientUtils.postData(url + urlParams, encryptedXmlBodyText);
  35. }catch (Exception e) {
  36. // ignore
  37. }
  38. }
  39. return "success";
  40. }
  41. /**
  42. * 接收微信消息与事件通知
  43. * 开发配置-开发资料-消息与事件接收配置
  44. */
  45. @PostMapping("/msgEvent/{appId}")
  46. public String receptionMsgEvent(@RequestBody String requestBody, @PathVariable String appId) {
  47. LOGGER.info("微信通知转发服务-收到微信消息与事件通知-{}", requestBody);
  48. return "success";
  49. }
  50. }