小灰灰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);
}
}
求解决
求解决
求解决方法
我也是 啊
我也是一直验证失败,你解决了么?
初识Java微信公众号开发
158335 学习 · 469 问题
相似问题