我们进入微信公众平台查看开发者文档,里面有详细的接入指南的步骤介绍,接下来我们逐步实现这个过程
1,首先我们要接受微信后台传到服务器上的四个参数,分别是signature(微信加密签名),timestamp(时间戳),nonce(随机数),echostr(随机字符串),新建一个项目(wechatconnect),将这个步骤放到com.example.wechat包下,这个包下新建一个servlet,在doget方法中接受后台传递过来的参数
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zmt.weixin.CheckUtil;
/**
* Servlet implementation class WeixinChat
*/
@WebServlet("/WeixinChat")
public class WeixinChat extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public WeixinChat() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String signature = request.getParameter("signature");//微信加密签名
String nonce = request.getParameter("nonce");//随机数
String echostr = request.getParameter("echostr");//随机字符串
String timestamp = request.getParameter("timestamp");//时间戳
PrintWriter out = response.getWriter();
if(CheckUtil.chatCheck(signature, timestamp, nonce)){
out.println(echostr);
}
}
}
2,加密/校验流程如下:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
讲个个步骤中的操作放到com.example.util包中,新建一个类weichatutil
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class CheckUtil {
private static final String token = "123zmystatic123";
public static boolean chatCheck(String signature, String timestamp, String nonce){
String[] arr = new String[]{token, timestamp, nonce};
Arrays.sort(arr);
StringBuffer content = new StringBuffer();
//练成字符串
for (String string : arr) {
content.append(string);
}
//sha1加密
String tmp = SHA(content.toString());
return tmp.equals(signature);
}
public static String SHA(String decript) {
try {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
}
3,最后要在web.xml文件中完成服务器加载的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>weixincon</display-name>
<servlet>
<servlet-name>weixin</servlet-name>
<servlet-class>com.zmt.util.WeixinChat</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>weixin</servlet-name>
<url-pattern>/weixin.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
接入指南就完成了