最近開始自學微信公衆平臺開發,記錄一下自己學習的東西,方便自己溫故,也希望能幫助到別人。
服務器配置
注意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;
}
}
}
}
}