위챗 공중 플랫폼 개발에 접속
3405 단어 Wechat
/**
*
* 1. ;
* a. , GET URL ;
* 2.
* a. signature , GET ,
* echostr , , , ;
* 3. : ;
* @param request
* @param response
* @throws IOException
*/
@RequestMapping(value="/start",method = RequestMethod.GET)
public void start(HttpServletRequest request,HttpServletResponse response) throws IOException {
//
if(wechatUtil.check(request)) {
PrintWriter writer = response.getWriter();
writer.write( request.getParameter("echostr"));
writer.flush();
writer.close();
System.out.println(" ");
}else {
System.out.println(" ");
}
}
/**
*
* @param timestamp
* @param nonce
* @param signature
* @param token
* @return
*/
public boolean check(HttpServletRequest request) {
// ,signature token timestamp 、nonce 。
String signature = request.getParameter("signature");
//
String timestamp = request.getParameter("timestamp");
//
String nonce = request.getParameter("nonce");
//
String echostr = request.getParameter("echostr");
System.out.println(signature+"
"+timestamp+"
"+nonce+"
"+echostr);
//1. token、timestamp、nonce
String [] str = {TOKEN,timestamp,nonce};
Arrays.sort(str);
//2) sha1
String strs = str[0]+str[1]+str[2];
String mysig= sha1(strs);
System.out.println("mysig:---->"+mysig);
//3) signature ,
return mysig.equals(signature);
}
/**
*
* @param strs
* @return
*/
public String sha1(String strs) {
// TODO Auto-generated method stub
try {
//
MessageDigest md = MessageDigest.getInstance("sha1");
//
byte[] digest = md.digest(strs.getBytes());
//
char [] chars = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append((chars)[(b>>4)&15]);
sb.append(chars[b&15]);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}