82年农夫山泉
2016-02-20 13:08
为什么代码一模一样还是不能通过开发者模式验证,显示: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算法。");
}
}
我也是同样问题你解决了没有
问题不在代码,每次使用ngrok你都要在命令环境里运行那条命令然后映射成功的那个界面不要关掉就可以提交成功了,前提是你的代码确实没有问题
是不是没有设置权限啊.
初识Java微信公众号开发
158318 学习 · 659 问题
相似问题