Java实现接入微信公众号服务器
在微信公众号开发中,我们需要接入微信服务器,让微信服务器与我们自己的服务器建立安全信道,建立信任。微信服务器向我们的服务器发送请求的时候会携带三个参数timestamp=时间戳&nonce=随机字符串&signature=签名。我们可以通过timestamp和nonce以及token来算出一个signature与传入的签名比较,如果相等则是微信合法请求,否则就不是合法请求。token(令牌)是微信端生成的,只有我们自己和微信知道,token不泄露给他人,他人是伪造不了微信请求的。本文将介绍以下内容:“在微信公众平台的服务器配置里配置好我们的服务器接口”、“获取微信服务器传递过来的参数”、“将token、timestamp、nonce三个参数进行字典序排序”、“将三个参数字符串拼接成一个字符串进行sha1加密”、“开发者获得加密后的字符串可与signature对比,标识该请求来源于微信”。
1、在微信公众平台的服务器配置里配置好我们的服务器接口。
/** 建立微信服务器与NBR服务器的安全信道,让它们建立信任 请求由微信服务器发送到NBR。 */
@RequestMapping(method = { RequestMethod.GET })
public void connectWxServerToNBR(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
if (!canCallCurrentAction(request.getSession(), BaseAction.EnumUserScope.ANYONE.getIndex())) {
logger.debug("无权访问本Action");
return;
}
logger.info("微信服务器开始建立与NBR的可信信道...");
……
2、获取微信服务器传递过来的参数。
获取微信加密的签名、时间戳、随机值、随机字符串。
// 获取WX服务端Get的值
String signature = request.getParameter(BaseWxModel.WX_SIGNATURE);// 微信加密的签名
String timestamp = request.getParameter(BaseWxModel.WX_TIMESTAMP);// 时间戳
String nonce = request.getParameter(BaseWxModel.WX_NONCE);// 随机值
String echostr = request.getParameter(BaseWxModel.WX_ECHOSTR);// 随机字符串
if (signature == null || timestamp == null || nonce == null || echostr == null) {
logger.info("非法的微信公众号服务器请求!");
return;
}
3、将token、timestamp、nonce三个参数进行字典序排序。
String[] array = new String[] { TOKEN, timestamp, nonce };
Arrays.sort(array);
4、将三个参数字符串拼接成一个字符串进行sha1加密。
StringBuffer content = new StringBuffer();
for (String b : array) {
content.append(b);
}
String encrypted = SHA1Util.SHA1(content.toString());
Sha1方法:
public final static String SHA1(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 {
// 获得SHA1摘要算法的 MessageDigest 对象s
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
// 使用指定的字节更新摘要
mdTemp.update(str.getBytes("UTF-8"));
// 获得密文
byte[] md = mdTemp.digest();
int len = md.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文转换成十六进制的字符串形式
for (int i = 0; i < len; i++) {
byte byte0 = md[i];
buf.append(hexDigits[byte0 >>> 4 & 0xf]);
buf.append(hexDigits[byte0 & 0xf]);
}
return buf.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
5、开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
if (encrypted.equals(signature)) {
out.print(echostr);
out.flush();
out.close();
logger.info("接入微信公众号服务器成功");
} else {
logger.info("接入微信公众号服务器失败");
}

浙公网安备 33010602011771号