1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 公众号与服务器验证失败 微信公众号服务器配置token验证失败原因

公众号与服务器验证失败 微信公众号服务器配置token验证失败原因

时间:2023-04-29 02:00:15

相关推荐

公众号与服务器验证失败 微信公众号服务器配置token验证失败原因

零、 原因

返回给微信平台的字符串两边加了引号(JSON序列化问题)

一、背景

半年前接触微信公众号开发,由于使用java,便直接使用最简单的Servlet去做校验,通过百度完美的成功接入服务器配置,后来接触到了一个小框架wxtools,对微信的api做了一层java的封装,无需自己写,就把原先写的改成了wxtools的。但项目上线的时候,问题出现了,配置服务器的时候——token验证失败。

二、过程

对配置文件一通排查,问题依然,调试的时候发现请求是接受到并成功返回了的,但微信那边依然显示token验证失败,然后百度,就发现有的人遇到和我一样的问题,他是因为返回的字符串带了双引号…

三、解决方法

错误代码

@GetMapping

@ResponseBody

public String check(String signature, String timestamp, String nonce, String echostr,HttpServletRequest request,HttpServletResponse response) {

log.info("服务器验证");

log.info("参数:signature:{} timestamp:{} nonce:{} echostr:{}",signature,timestamp,nonce,echostr);

if (iService.checkSignature(signature, timestamp, nonce, echostr)) {

log.info("校验成功:{}",echostr);

return echostr;

}

}

这里怀疑是JSON序列化的时候往两边加上了双引号,没有做验证,有兴趣可以验证一下,我使用的是FastJSON

正确打开方式

@GetMapping

@ResponseBody

public void check(String signature, String timestamp, String nonce, String echostr,HttpServletRequest request,HttpServletResponse response) {

log.info("服务器验证");

log.info("参数:signature:{} timestamp:{} nonce:{} echostr:{}",signature,timestamp,nonce,echostr);

if (iService.checkSignature(signature, timestamp, nonce, echostr)) {

log.info("校验成功:{}",echostr);

try {

// 直接使用response

response.getWriter().print(echostr);

} catch (IOException e) {

e.printStackTrace();

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。