美文网首页
微信公众号返回echostr验证失败问题

微信公众号返回echostr验证失败问题

作者: 板栗炖牛肉 | 来源:发表于2021-11-04 15:56 被阅读0次

前言

  • 验证服务器需要原样返回echostr字符串
  • 开发环境spring boot

解决方案

  • contentType改为text/plain;charset=UTF-8即可

  • 代码贴出

org.apache.commons.codec.digest.DigestUtils;

...

    @GetMapping("/sign")
    public void getSignInfo(@RequestParam(value = "signature") String signature,
                              @RequestParam("timestamp") String timestamp,
                              @RequestParam("nonce") String nonce,
                              @RequestParam("echostr") String echostr, HttpServletResponse httpServletResponse) throws Exception {
        List<String> array = new ArrayList<>();
        array.add(TOKEN);
        array.add(nonce);
        array.add(timestamp);
        Collections.sort(array, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        String arrStr = "";
        for (String str : array) {
            arrStr += str;
        }
        String sign = DigestUtils.sha1Hex(arrStr);
        if (!sign.equals(signature)) {
            RestCodeType.USER_ACCESS_ERROR.write();
        }
        httpServletResponse.setContentType("text/plain;charset=UTF-8");
        httpServletResponse.getOutputStream().write(echostr.getBytes());
        httpServletResponse.getOutputStream().close();
    }
  • 或者
org.apache.commons.codec.digest.DigestUtils;

...
    @GetMapping(value = "/sign", produces = "text/plain;charset=UTF-8")
    public String getSignInfo(@RequestParam(value = "signature") String signature,
                              @RequestParam("timestamp") String timestamp,
                              @RequestParam("nonce") String nonce,
                              @RequestParam("echostr") String echostr, HttpServletResponse httpServletResponse) throws Exception {
        List<String> array = new ArrayList<>();
        array.add(TOKEN);
        array.add(nonce);
        array.add(timestamp);
        Collections.sort(array, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        String arrStr = "";
        for (String str : array) {
            arrStr += str;
        }
        String sign = DigestUtils.sha1Hex(arrStr);
        if (!sign.equals(signature)) {
            RestCodeType.USER_ACCESS_ERROR.write();
        }
        return echostr;
    }

相关文章

网友评论

      本文标题:微信公众号返回echostr验证失败问题

      本文链接:https://www.haomeiwen.com/subject/eidkzltx.html