最近开始自学微信公众平台开发,记录一下自己学习的东西,方便自己温故,也希望能帮助到别人。
服务器配置
注意URL服务器地址 外网的80端口,Token 随便填写,微信服务器加密时候会带上这个值,与验证方法的token匹配即可。
服务器地址验证方法
package com.qs.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import com.qs.util.SignUtil;
/**
* @author qiaoshuai
* @createDate 2016-3-1
*/
public class TestWeiXinAction extends ActionSupport implements
ServletRequestAware, ServletResponseAware {
private static final long serialVersionUID = -799327593656049028L;
protected HttpServletRequest request;
private HttpServletResponse response;
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void checkSignature() throws IOException {
// 微信加密签名
String signature = request.getParameter("signature");
System.out.println("signature=" + signature);
// 时间戳
String timestamp = request.getParameter("timestamp");
System.out.println("timestamp=" + timestamp);
// 随机数
String nonce = request.getParameter("nonce");
System.out.println("nonce=" + nonce);
// 随机字符串
String echostr = request.getParameter("echostr");
System.out.println("echostr=" + echostr);
PrintWriter out = response.getWriter();
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}
out.close();
out = null;
}
}
package com.qs.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 请求校验工具类
*/
public class SignUtil {
// 与接口配置信息中的Token要一致
private static String token = "qs";
public static boolean checkSignature(String signature, String timestamp,
String nonce) {
// 从请求中(也就是微信服务器传过来的)拿到的token, timestamp, nonce
String[] arr = new String[] { token, timestamp, nonce };
// 将token、timestamp、nonce三个参数进行字典序排序
sort(arr);
StringBuilder content = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
content.append(arr[i]);
}
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
// 将三个参数字符串拼接成一个字符串进行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
// 将字节数组转成字符串
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
content = null;
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
}
// 将加密后的字节数组变成字符串
private static String byteToStr(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
private static String byteToHexStr(byte mByte) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] tempArr = new char[2];
tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
tempArr[1] = Digit[mByte & 0X0F];
String s = new String(tempArr);
return s;
}
// 用于字典排序
public static void sort(String a[]) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[j].compareTo(a[i]) < 0) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
本文介绍微信公众平台的服务器配置步骤及验证方法,包括如何设置正确的URL、Token等参数,并提供了一个具体的Java示例代码来演示如何实现服务器地址的验证。
1万+

被折叠的 条评论
为什么被折叠?



