原文地址:https://blog.csdn.net/mzmzo/article/details/50991616
package com.javen.weixin.controller;
import java.security.MessageDigest;
import java.util.Arrays;
import org.apache.commons.codec.binary.Hex;
import com.jfinal.core.Controller;
public class TestWeixinController extends Controller{
public void index() throws Exception{
// 开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带参数
String signature = getPara("signature");// 微信加密签名(token、timestamp、nonce。)
String timestamp = getPara("timestamp");// 时间戳
String nonce = getPara("nonce");// 随机数
String echostr = getPara("echostr");// 随机字符串
//PrintWriter out = response.getWriter();
// 将token、timestamp、nonce三个参数进行字典序排序
String[] params = new String[] { "yourToken", timestamp, nonce };
Arrays.sort(params);
// 将三个参数字符串拼接成一个字符串进行sha1加密
String clearText = params[0] + params[1] + params[2];
String algorithm = "SHA-1";
String sign = new String(
Hex.encodeHex(MessageDigest.getInstance(algorithm).digest((clearText).getBytes()), true));
// 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
if (signature.equals(sign)) {
//response.getWriter().print(echostr);
renderJson(echostr);
}
}
}
原文地址:https://blog.csdn.net/mzmzo/article/details/50991616