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算法。");
?
}
}
2017-03-25
我也是同樣問題你解決了沒有
2016-05-11
問題不在代碼,每次使用ngrok你都要在命令環境里運行那條命令然后映射成功的那個界面不要關掉就可以提交成功了,前提是你的代碼確實沒有問題
2016-02-25
是不是沒有設置權限啊.