controller
@RequestMapping("/weixin")
@ResponseBody
public String weixin(String signature,String timestamp
,String nonce,String echostr) throws NoSuchAlgorithmException {
String token="umaiw";
String tmpStr= getSHA1(token, timestamp, nonce);
System.out.println("+++++++++++++++++++++tmpStr "+tmpStr);
System.out.println("---------------------signature "+signature);
if(tmpStr.equals(signature)){
return echostr;
}else{
return null;
}
/**
* 用SHA1算法生成安全签名
* @param token 票据
* @param timestamp 时间戳
* @param nonce 随机字符串
* @param encrypt 密文
* @return 安全签名
* @throws NoSuchAlgorithmException
* @throws AesException
*/
public String getSHA1(String token, String timestamp, String nonce) throws NoSuchAlgorithmException {
String[] array = new String[] { token, timestamp, nonce };
StringBuffer sb = new StringBuffer();
// 字符串排序
Arrays.sort(array);
for (int i = 0; i
sb.append(array[i]);
}
String str = sb.toString();
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
StringBuffer hexstr = new StringBuffer();
String shaHex = "";
for (int i = 0; i
shaHex = Integer.toHexString(digest[i] & 0xFF);
if (shaHex.length()
hexstr.append(0);
}