83,微信token验证失败的解决方法

一、问题由来
在使用URL和Token启用微信公众平台开发模式消息接口的时候,我们会碰到下面三种情况

1. token校验失败


image

这样回头检查一下各项配置是否正确。如果确定配置没有问题,请按下面的方法检查

2. 请求URL超时

image

你的服务器在国外,或者服务器网速不给力,一般多试几次就可以了。如果经常这样,就需要考虑更换服务器

3. 提交成功

image

恭喜你,配置成功了。

我们来讲解一下第一种的原因及解决方法。

package com.taotao.weiixin.controller;

import com.taotao.weiixin.util.CheckUtil;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.PrintWriter;
import java.util.Arrays;

@RestController
public class IndexController {
    private static final String token = "helloworld"; //token

    @RequestMapping(value = "/weChat", method = RequestMethod.GET)
    @ResponseBody
    public String validate(String signature, String timestamp, String nonce, String echostr) {
        String[] arr = new String[]{token, timestamp, nonce};
        //排序
        Arrays.sort(arr);
        //生成字符串
        StringBuffer content = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);//拼接字符串
        }
        String mySignature = CheckUtil.shal(content.toString());
        if (!"".equals(signature) && !"".equals(mySignature) && signature.equals(mySignature)) {
            System.out.println("-----签名校验通过-----");
            return echostr;
        } else {
            System.out.println("-----校验签名失败-----");
            return null;
        }


    }


}



package com.taotao.weiixin.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * 加密校验
 */
public class CheckUtil {
    private static final String token = "helloworld"; //token

    public static boolean checkSignature(String signature, String timestamp, String nonce) throws Exception {
        String[] arr = new String[]{token, timestamp, nonce};
          //排序
        Arrays.sort(arr);
        //生成字符串
        StringBuffer content =new StringBuffer();
        for (int i = 0; i 

这里用的是飞鸽内网穿透 https://neiwangchuantou.vip/help/

你可能感兴趣的:(83,微信token验证失败的解决方法)