WeCallBackXmlController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.linkwechat.controller;
  2. import com.linkwechat.common.exception.AesException;
  3. import com.linkwechat.common.utils.StringUtils;
  4. import com.linkwechat.common.utils.wecom.WxCryptUtil;
  5. import com.linkwechat.common.utils.xml.XmlUtils;
  6. import com.linkwechat.domain.WeCorpAccount;
  7. import com.linkwechat.service.ISxkWelcomeMsgService;
  8. import com.linkwechat.service.IWeCorpAccountService;
  9. import com.linkwechat.service.IXmlMsgService;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiModelProperty;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.context.annotation.Lazy;
  15. import org.springframework.web.bind.annotation.*;
  16. import javax.servlet.http.HttpServletRequest;
  17. /**
  18. * @author Shixk
  19. * @description 企微回调
  20. * @date 2023/11/1 11:31
  21. **/
  22. @Api(tags = "企微回调通知")
  23. @Slf4j
  24. @RestController
  25. @RequestMapping("/wecom/callback/xml")
  26. public class WeCallBackXmlController {
  27. @Lazy
  28. @Autowired
  29. private IXmlMsgService xmlMsgService;
  30. @Autowired
  31. private IWeCorpAccountService weCorpAccountService;
  32. @ApiModelProperty("post数据接收")
  33. @PostMapping(value = "/recive/{corpId}")
  34. public String recive(@RequestBody String msg, @RequestParam(name = "msg_signature") String signature,
  35. String timestamp, String nonce, @PathVariable("corpId") String corpId) {
  36. log.info("XML-post数据接收>>>>>>>>>>corpId:{},msg:{}", corpId, msg);
  37. log.info("XML-post参数>>>>>>>>>>msg_signature:{}==timestamp:{}==nonce:{}==corpId:{}==msg:{}",
  38. signature, timestamp, nonce, corpId, msg);
  39. WeCorpAccount weCorpAccount = weCorpAccountService.getCorpAccountByCorpId(corpId);
  40. WxCryptUtil wxCryptUtil = new WxCryptUtil(weCorpAccount.getToken(), weCorpAccount.getEncodingAesKey(), corpId);
  41. try {
  42. String decrypt = wxCryptUtil.decrypt(signature, timestamp, nonce, msg);
  43. log.info("XML-----------------post数据解密后:{}", decrypt);
  44. String resDate = xmlMsgService.msgHandle(decrypt);
  45. log.info("XML-----------------逻辑完成后返回数据[1]:{}", resDate);
  46. String res = wxCryptUtil.encrypt(resDate);
  47. log.info("XML-----------------最终返回数据[3]:{}", res);
  48. return res;
  49. } catch (Exception e) {
  50. log.error("XML-post数据接收>>>>>>error", e);
  51. String sRespData = WxCryptUtil.getTextRespData("success");
  52. return wxCryptUtil.encrypt(sRespData);
  53. }
  54. }
  55. @ApiModelProperty("get数据校验")
  56. @GetMapping(value = "/recive/{corpId}")
  57. public String recive(HttpServletRequest request, @PathVariable("corpId") String corpId) {
  58. log.info("XML-get数据校验>>>>>>>>>>corpId:{}", corpId);
  59. // 微信加密签名
  60. String sVerifyMsgSig = request.getParameter("msg_signature");
  61. // 时间戳
  62. String sVerifyTimeStamp = request.getParameter("timestamp");
  63. // 随机数
  64. String sVerifyNonce = request.getParameter("nonce");
  65. // 随机字符串
  66. String sVerifyEchoStr = request.getParameter("echostr");
  67. log.info("XML-get参数>>>>>>>>>>msg_signature:{}==timestamp:{}==nonce:{}==sVerifyEchoStr:{}",
  68. sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr);
  69. WeCorpAccount weCorpAccount = weCorpAccountService.getCorpAccountByCorpId(corpId);
  70. WxCryptUtil wxCryptUtil = new WxCryptUtil(weCorpAccount.getToken(), weCorpAccount.getEncodingAesKey(), corpId);
  71. try {
  72. if (StringUtils.isEmpty(sVerifyEchoStr)) {
  73. return "success";
  74. }
  75. String s = wxCryptUtil.verifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr);
  76. log.info("XML-----------------GET数据解密后:{}", s);
  77. return s;
  78. } catch (Exception e) {
  79. log.error("XML-get数据校验>>>>>>error", e);
  80. return "error";
  81. }
  82. }
  83. public static void main(String[] args) throws AesException {
  84. String sVerifyMsgSig = "1b7276a1856ac52dc7ae408d3ee1b923780f60ca";
  85. String sVerifyTimeStamp = "1704793285";
  86. String sVerifyNonce = "1704681796";
  87. String sVerifyEchoStr = "LoWs4zLC+WzW7u5lpaHlIMD7i0fnIN7rek4L8hj1vpXxvG9oDu4aInFfQ7ZRTok1P2592kNOfeBug37xyIgLJQ==";
  88. WxCryptUtil wxCryptUtil = new WxCryptUtil("dPvUu3mF2DBw559i0RhMGNFkKDi", "iUBcItTG64fjIjbMDNPqLIoeLjYdzOo4ghVHonb7wNB", "wx73e9b369d87bf285");
  89. String s = wxCryptUtil.verifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr);
  90. System.out.println(s);
  91. }
  92. }