forked from chanjarster/weixin-java-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
weixin java mp4.5.0 版本,使用 WxCpMessageRouter.route(微信消息); 报错问题处理
Binary Wang edited this page Jan 30, 2024
·
2 revisions
使用时,请参考demo这里面
WxCpService wxCpService = new WxCpServiceImpl(); //这里要缓存
wxCpService.setWxCpConfigStorage(config); //传入微信配置信息不做重复
WxCpMessageRouter wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
一般情况下 wxCpService 会作为缓存注入spring容器中. 故使用WxCpMessageRouter.route
时,建源码: me.chanjar.weixin.mp.api.WxMpMessageRouter#route(me.chanjar.weixin.mp.bean.message.WxMpXmlMessage, java.util.Map<java.lang.String,java.lang.Object>, me.chanjar.weixin.mp.api.WxMpService)
212行
这里因为WxMpConfigStorageHolder.THREAD_LOCAL为set值,返回默认值default肯定是拿不到缓存的appid对应的wxCpService.
解决办法: 在自己程序代码使用wxCpMessageRouter.route前面,加上 WxMpConfigStorageHolder.THREAD_LOCAL.set(当前操作的appId)
String encType = request.getParameter("encrypt_type");
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
String msgSignature = request.getParameter("msg_signature");
String signature = request.getParameter("signature");
String openid = request.getParameter("openid");
log.info("接收微信请求:[openid=[{}], [openid=[{}],[signature=[{}], encType=[{}], msgSignature=[{}],"
+ " timestamp=[{}], nonce=[{}], requestBody=[{}] ",
appid, openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
// 请求里面没有对应信息。 // if (!this.wxMpService.switchover(appid)) { //appid // throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid)); // }
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
}
return ResponseEntity.ok("");