package com.linkwechat.controller; import com.linkwechat.common.exception.AesException; import com.linkwechat.common.utils.StringUtils; import com.linkwechat.common.utils.wecom.WxCryptUtil; import com.linkwechat.common.utils.xml.XmlUtils; import com.linkwechat.domain.WeCorpAccount; import com.linkwechat.service.ISxkWelcomeMsgService; import com.linkwechat.service.IWeCorpAccountService; import com.linkwechat.service.IXmlMsgService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModelProperty; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; /** * @author Shixk * @description 企微回调 * @date 2023/11/1 11:31 **/ @Api(tags = "企微回调通知") @Slf4j @RestController @RequestMapping("/wecom/callback/xml") public class WeCallBackXmlController { @Lazy @Autowired private IXmlMsgService xmlMsgService; @Autowired private IWeCorpAccountService weCorpAccountService; @ApiModelProperty("post数据接收") @PostMapping(value = "/recive/{corpId}") public String recive(@RequestBody String msg, @RequestParam(name = "msg_signature") String signature, String timestamp, String nonce, @PathVariable("corpId") String corpId) { log.info("XML-post数据接收>>>>>>>>>>corpId:{},msg:{}", corpId, msg); log.info("XML-post参数>>>>>>>>>>msg_signature:{}==timestamp:{}==nonce:{}==corpId:{}==msg:{}", signature, timestamp, nonce, corpId, msg); WeCorpAccount weCorpAccount = weCorpAccountService.getCorpAccountByCorpId(corpId); WxCryptUtil wxCryptUtil = new WxCryptUtil(weCorpAccount.getToken(), weCorpAccount.getEncodingAesKey(), corpId); try { String decrypt = wxCryptUtil.decrypt(signature, timestamp, nonce, msg); log.info("XML-----------------post数据解密后:{}", decrypt); String resDate = xmlMsgService.msgHandle(decrypt); log.info("XML-----------------逻辑完成后返回数据[1]:{}", resDate); String res = wxCryptUtil.encrypt(resDate); log.info("XML-----------------最终返回数据[3]:{}", res); return res; } catch (Exception e) { log.error("XML-post数据接收>>>>>>error", e); String sRespData = WxCryptUtil.getTextRespData("success"); return wxCryptUtil.encrypt(sRespData); } } @ApiModelProperty("get数据校验") @GetMapping(value = "/recive/{corpId}") public String recive(HttpServletRequest request, @PathVariable("corpId") String corpId) { log.info("XML-get数据校验>>>>>>>>>>corpId:{}", corpId); // 微信加密签名 String sVerifyMsgSig = request.getParameter("msg_signature"); // 时间戳 String sVerifyTimeStamp = request.getParameter("timestamp"); // 随机数 String sVerifyNonce = request.getParameter("nonce"); // 随机字符串 String sVerifyEchoStr = request.getParameter("echostr"); log.info("XML-get参数>>>>>>>>>>msg_signature:{}==timestamp:{}==nonce:{}==sVerifyEchoStr:{}", sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr); WeCorpAccount weCorpAccount = weCorpAccountService.getCorpAccountByCorpId(corpId); WxCryptUtil wxCryptUtil = new WxCryptUtil(weCorpAccount.getToken(), weCorpAccount.getEncodingAesKey(), corpId); try { if (StringUtils.isEmpty(sVerifyEchoStr)) { return "success"; } String s = wxCryptUtil.verifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr); log.info("XML-----------------GET数据解密后:{}", s); return s; } catch (Exception e) { log.error("XML-get数据校验>>>>>>error", e); return "error"; } } public static void main(String[] args) throws AesException { String sVerifyMsgSig = "1b7276a1856ac52dc7ae408d3ee1b923780f60ca"; String sVerifyTimeStamp = "1704793285"; String sVerifyNonce = "1704681796"; String sVerifyEchoStr = "LoWs4zLC+WzW7u5lpaHlIMD7i0fnIN7rek4L8hj1vpXxvG9oDu4aInFfQ7ZRTok1P2592kNOfeBug37xyIgLJQ=="; WxCryptUtil wxCryptUtil = new WxCryptUtil("dPvUu3mF2DBw559i0RhMGNFkKDi", "iUBcItTG64fjIjbMDNPqLIoeLjYdzOo4ghVHonb7wNB", "wx73e9b369d87bf285"); String s = wxCryptUtil.verifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr); System.out.println(s); } }