1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| @RestController @RequestMapping("/portal/WxMpAccount/{id}") public class WxPortalController {
@Autowired private WxMpServiceHelper wxMpServiceHelper;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static final String ENCRYPT_TYPE_AES = "aes";
@ResponseBody @GetMapping(produces = "text/plain; charset=utf-8") public String authGet(@PathVariable Long id, @RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("echostr") String echostr) { logger.info("\n接收到来自微信服务器的认证消息:[{},{},{},{}]", signature, timestamp, nonce, echostr); if (wxMpServiceHelper.wxMpService(id).checkSignature(timestamp, nonce, signature)) { return echostr; } return "非法请求"; }
@ResponseBody @PostMapping(produces = "application/xml; charset=utf-8") public String post(@PathVariable Long id, @RequestBody String requestBody, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("signature") String signature, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature",required = false) String msgSignature) { logger.debug("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}], timestamp=[{}], nonce=[{}], " + "requestBody=[\n{}\n] ", signature, encType, msgSignature, timestamp, nonce, requestBody); if (!wxMpServiceHelper.wxMpService(id).checkSignature(timestamp, nonce, signature)) { throw new IllegalArgumentException("非法请求,可能属于伪造的请求!"); } String out = null; if (encType == null) { WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody); WxMpXmlOutMessage outMessage = wxMpServiceHelper.wxMpMessageRouter(id).route(inMessage); out = outMessage == null ? "" : outMessage.toXml(); } else if (ENCRYPT_TYPE_AES.equalsIgnoreCase(encType)) { WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxMpServiceHelper.wxMpService(id).getWxMpConfigStorage(), timestamp, nonce, msgSignature); logger.debug("\n消息解密后内容为:\n{}", inMessage.toString()); WxMpXmlOutMessage outMessage = wxMpServiceHelper.wxMpMessageRouter(id).route(inMessage); out = outMessage == null ? "" : outMessage.toEncryptedXml(wxMpServiceHelper.wxMpService(id).getWxMpConfigStorage()); } logger.debug("\n回复信息:{}", out); return out; } }
|