1. 程式人生 > >微信公眾號 Token校驗失敗 基於spring-boot

微信公眾號 Token校驗失敗 基於spring-boot

最開始是這麼寫:

@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!!!