小程序
小游戏
企业微信
微信支付
扫描小程序码分享
本地使用postman,请求了要设置在微信公众号服务器回调的URL,可以正常返回echostr,点击提交,一直报错token验证失败,请问这个是什么原因呢?
9 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
正确返回了微信 服务器需要的echostr,还是提示token验证失败
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
如果thinkphp框架,是有个巨坑的,同一段代码,独立文件就可以,放到框架里就不行了,
由于thinkphp开启了 debug模式 ,导致每次请求都会暗地里输出一些debug信息,关上就好了
返回类型不能是string!!!使用php返回了string类型,结果一直失败,改成int后,终于成功了!
package com.example.hyh; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @RestController public class HelloController { @RequestMapping(value = "/wxcheck", method = RequestMethod.GET) public void check(HttpServletRequest request, HttpServletResponse response) throws IOException { String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); if (SignUtil.checkSignature(signature, timestamp, nonce)) { response.getWriter().write(echostr); // 直接写入响应体 } else { response.getWriter().write("验证失败"); } } } package com.example.hyh; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; public class SignUtil { // 与微信公众平台配置的Token一致 private static final String TOKEN = "hyh"; public static boolean checkSignature(String signature, String timestamp, String nonce) { // 检查参数是否为空 if (signature == null || timestamp == null || nonce == null) { return false; } // 1. 将token、timestamp、nonce按字典序排序 String[] arr = new String[]{TOKEN, timestamp, nonce}; Arrays.sort(arr); // 2. 将三个参数字符串拼接成一个字符串 StringBuilder content = new StringBuilder(); for (String s : arr) { content.append(s); } // 3. 对拼接后的字符串进行SHA-1加密 String encryptedStr = sha1(content.toString()); // 4. 将加密后的字符串与signature对比 return encryptedStr != null && encryptedStr.equals(signature); } private static String sha1(String str) { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] digest = md.digest(str.getBytes()); return byteToHexStr(digest); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("SHA-1算法不可用", e); } } private static String byteToHexStr(byte[] byteArray) { StringBuilder hexStr = new StringBuilder(); for (byte b : byteArray) { String hex = Integer.toHexString(b & 0xFF); if (hex.length() == 1) { hexStr.append("0"); } hexStr.append(hex); } return hexStr.toString(); } }用的ngrok,一直token验证失败
不要返回对象,直接返回这个字符串就可以成功了
大神,求助,我按照你上面的操作试了还是不行,请求支援。
使用示例代码hashcode = sha1.hexdigest() 生成的hashcode始终不变,无法与微信传过来的signature匹配,为什么呢
list = [token, timestamp, nonce]
list.sort()
tmp_str = ''.join(list)
hashcode = hashlib.sha1(tmp_str.encode('utf-8')).hexdigest()
另外有个问题想问下,你是怎么知道是要将echo返回的写在response里面呢
如果微信给你返回的是echostr=null,signature=null,timestamp=null,nonce=null,你会怎么处理?
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
正确返回了微信 服务器需要的echostr,还是提示token验证失败
1.检查,request 是不是 UTF-8,避免获取的数据是乱码
2.response.setContentType("application/json;charset=UTF-8"),并且设置编码 response.setCharacterEncoding("UTF-8")
3.也是最重要的一点,接口方法要设置为void,不要返回String,因为微信公众号验证签名的那个URL,人家要求返回echostr,但是这个echostr是要输出在response.getWriter().write(echostr)中的,不要直接 retrun echostr,(这里是真的坑,卡我好久)
4.加密的几种方式,第一种明文,第二种兼容,第三种安全模式,重点说一下,token验证的时候 微信公众号 服务器发送的数据是不同的,大致如下:
(1)明文,signature、timeStamp、nonce和echostr都是空
(2)兼容模式和安全模式下,signature、timeStamp、nonce都是有值的,echostr是空。
区别:1.token验证和兼容模式和安全模式的,signature、timeStamp、nonce都是有值的,区别在于,token验证的时候echostr是有值的,而安全模式下和兼容模式下,微信给我们接口发消息的时候echostr是没有值的 2.明文模式和安全模式和兼容模式的区别在于,四个字段是否有值,可以作为判断依据。判断是否哦要加解密
大概就这么多吧,一步一个坑,踩过来的,希望能帮到大家,少踩坑,快速解决问题。
如果thinkphp框架,是有个巨坑的,同一段代码,独立文件就可以,放到框架里就不行了,
由于thinkphp开启了 debug模式 ,导致每次请求都会暗地里输出一些debug信息,关上就好了
返回类型不能是string!!!使用php返回了string类型,结果一直失败,改成int后,终于成功了!
package com.example.hyh; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @RestController public class HelloController { @RequestMapping(value = "/wxcheck", method = RequestMethod.GET) public void check(HttpServletRequest request, HttpServletResponse response) throws IOException { String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); if (SignUtil.checkSignature(signature, timestamp, nonce)) { response.getWriter().write(echostr); // 直接写入响应体 } else { response.getWriter().write("验证失败"); } } } package com.example.hyh; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; public class SignUtil { // 与微信公众平台配置的Token一致 private static final String TOKEN = "hyh"; public static boolean checkSignature(String signature, String timestamp, String nonce) { // 检查参数是否为空 if (signature == null || timestamp == null || nonce == null) { return false; } // 1. 将token、timestamp、nonce按字典序排序 String[] arr = new String[]{TOKEN, timestamp, nonce}; Arrays.sort(arr); // 2. 将三个参数字符串拼接成一个字符串 StringBuilder content = new StringBuilder(); for (String s : arr) { content.append(s); } // 3. 对拼接后的字符串进行SHA-1加密 String encryptedStr = sha1(content.toString()); // 4. 将加密后的字符串与signature对比 return encryptedStr != null && encryptedStr.equals(signature); } private static String sha1(String str) { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] digest = md.digest(str.getBytes()); return byteToHexStr(digest); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("SHA-1算法不可用", e); } } private static String byteToHexStr(byte[] byteArray) { StringBuilder hexStr = new StringBuilder(); for (byte b : byteArray) { String hex = Integer.toHexString(b & 0xFF); if (hex.length() == 1) { hexStr.append("0"); } hexStr.append(hex); } return hexStr.toString(); } }用的ngrok,一直token验证失败
不要返回对象,直接返回这个字符串就可以成功了
大神,求助,我按照你上面的操作试了还是不行,请求支援。
使用示例代码hashcode = sha1.hexdigest() 生成的hashcode始终不变,无法与微信传过来的signature匹配,为什么呢
list = [token, timestamp, nonce]
list.sort()
tmp_str = ''.join(list)
hashcode = hashlib.sha1(tmp_str.encode('utf-8')).hexdigest()
另外有个问题想问下,你是怎么知道是要将echo返回的写在response里面呢
如果微信给你返回的是echostr=null,signature=null,timestamp=null,nonce=null,你会怎么处理?