token检验失败

来源:5-3 消息的接收与响应

小灰灰20151314

2015-08-02 18:16

package com.huihui.util;


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

public class CheckUtil {
	private static final String token="huihui";
	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]);
		}
		//加密
		String temp=getSha1(content.toString());
		return temp.equals(signature);
	}
	public 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("SHA1");
			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;
		}

	}
}

package com.huihui.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 com.huihui.util.CheckUtil;


public class WeiXinServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

// TODO Auto-generated method stub

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

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

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

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


PrintWriter out=resp.getWriter();

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

out.print(echostr);

}

super.doGet(req, resp);

}

}

http://img.mukewang.com/55bdee070001e2b606630390.jpg

写回答 关注

5回答

  • qq_不在迷恋已逝的往事_03997394
    2017-07-29 20:42:50

    求解决


  • qq_似水流年_118
    2016-04-22 23:21:47

    求解决

  • Harck
    2016-04-20 13:54:45

    求解决方法

  • Justonedayzhupeng
    2015-10-23 16:22:24

    我也是 啊 

  • gyc1723
    2015-09-17 20:18:22

    我也是一直验证失败,你解决了么?

初识Java微信公众号开发

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

158330 学习 · 469 问题

查看课程

相似问题