微信公众号 Token校验失败 基于spring-boot

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a5nan/article/details/83030099

最开始是这么写:

@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("非法请求");
  }

}

通过了,应该是框架的问题,用return echostr; 输出和BufferedOutStream输出是不一样的。

fuck!!!

猜你喜欢

转载自blog.csdn.net/a5nan/article/details/83030099