【填个坑】微信Token验证失败原因及解决方案

微信公众平台修改服务器配置时token验证失败

网上很多都是php教程,
看这里

https://blog.csdn.net/sinat_22878395/article/details/69258165

还有下面这篇说的比较有高度

https://www.zkii.net/tech/php/813.html

我的项目用的Spring-boot 框架,使用了

班纳睿 

的全能微信Java开发工具包
https://gitee.com/binary/weixin-java-tools/tree/master

微信公众号demo,使用weixin-java-tools,基于Spring Boot

https://gitee.com/binary/weixin-java-mp-demo-springboot

其中处理微信服务器认证的方法如下,(仔细看,感觉没啥毛病)

 @GetMapping(produces = "text/plain;charset=utf-8")

  public String authGet(

      @RequestParam(name = "signature",  required = false) String signature,

      @RequestParam(name = "timestamp", required = false) String timestamp,

      @RequestParam(name = "nonce", required = false) String nonce,

      @RequestParam(name = "echostr", required = false) String echostr) {


    this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);

    if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
      throw new IllegalArgumentException("请求参数非法,请核实!");
    }

    if (this.wxService.checkSignature(timestamp, nonce, signature)) {
      return echostr;
    }
    return "非法请求";
  }

最好修改如下:

@GetMapping(produces = "text/plain;charset=UTF-8")
public void authGet(
    @RequestParam(name = "signature",
        required = false) String signature,
    @RequestParam(name = "timestamp",
        required = false) String timestamp,
    @RequestParam(name = "nonce", required = false) String nonce,
    @RequestParam(name = "echostr", required = false) String echostr,HttpServletResponse response ) throws IOException {
   

  this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature,
      timestamp, nonce, echostr);

  if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
    throw new IllegalArgumentException("请求参数非法,请核实!");
  }

  if (this.wxService.checkSignature(timestamp, nonce, signature)) {
    //------------------------------
    //处理业务完毕
    //------------------------------
    BufferedOutputStream out = new BufferedOutputStream(
            response.getOutputStream());
    out.write(echostr.getBytes());
    out.flush();
    out.close();
  } else {
    logger.info("非法请求");
  }

}

问题解决!  也许其他人 用weixin-java-tools 没有遇到这样的问题, 这里也可能跟服务器环境配置有关,具体详情暂不深挖(这个坑日后再填)。

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注