为了账号安全,请及时绑定邮箱和手机立即绑定

token验证失败,一模一样的东西

为什么代码一模一样还是不能通过开发者模式验证,显示:TOKEN验证失败!

几天都没看出来有什么问题,求解答,

//WeixinServlet.java文件

package servlet;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import toolsClass.CheckUtil;


public class WeixinServlet extends HttpServlet {


/**

* Constructor of the object.

*/

public WeixinServlet() {

super();

}


public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}


public void doDelete(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {


// Put your code here

}


public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


String signature = request.getParameter("signature");

String timestamp = request.getParameter("timestamp");

String nonce = request.getParameter("nonce");

String echostr = request.getParameter("echostr");

//返回随机字符串:echostr

PrintWriter out = response.getWriter();

if(CheckUtil.checkSignature(signature,timestamp,nonce)){

//

System.out.println("微信服务器身份验证通过");

out.print(echostr);

}

}


public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println("  <BODY>");

out.print("    This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println("  </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}


public void doPut(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


// Put your code here

}


public void init() throws ServletException {

// Put your code here

}


}

//CheckUtil.java文件

package toolsClass;


import java.security.MessageDigest;

import java.util.Arrays;


public class CheckUtil {

private static final String token = "Walter";

public static boolean checkSignature(String signature,String timestamp,String nonce){

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]); //append方法用来累积字符串的, 

}


//sha1加密

String temp = getSha1(content.toString());

return temp.equals(signature);


}

//sha1加密算法

static String  getSha1(String str){

if(str == null || str.length()==0)

return null;

char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

try{

MessageDigest mdTemp = MessageDigest.getInstance("SHA2");

mdTemp.update(str.getBytes("UTF-8"));

byte[] md = mdTemp.digest();

int j = md.length;

char buf[] = new char[j*2];

int k = 0;

for(int i=0;i<j;i++){

byte byte0 = md[i];

buf[k++] = hexDigits[byte0>>>4 & 0xf];

buf[k++] = hexDigits[byte0 & 0xf];

}

return new String(buf);

}catch(Exception e){

return null;

}

}

public static void main(String []argss){

System.out.println("===========SHA2:是SHA1算法。");

 

}

}

56c7f3e500010aa105000212.jpg

56c7f3e500014e5005000287.jpg


正在回答

3 回答

我也是同样问题你解决了没有


0 回复 有任何疑惑可以回复我~

问题不在代码,每次使用ngrok你都要在命令环境里运行那条命令然后映射成功的那个界面不要关掉就可以提交成功了,前提是你的代码确实没有问题

0 回复 有任何疑惑可以回复我~

是不是没有设置权限啊.

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
初识Java微信公众号开发
  • 参与学习       158313    人
  • 解答问题       684    个

Java微信公众号开发的入门教程,掌握微信公众号基本概念

进入课程

token验证失败,一模一样的东西

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号