一、实战开发
1、数据连接,因为是跟微信的服务器对接,所以需要做跟微信后端服务器的连接工作
2、 创建连接的类,因为是交互的所以首先建立一个weixin的servlet
1 package com.my.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import com.my.utils.CheckUtil; 10 11 //微信连接 12 public class WeXinServlet extends HttpServlet { 13 14 /** 15 * 随机生成的版本id 16 */ 17 private static final long serialVersionUID = 1L; 18 19 /** 20 * 开发文档第一部分与微信的连接方式代码 21 */ 22 @Override 23 protected void doGet(HttpServletRequest request, 24 HttpServletResponse response) throws ServletException, IOException { 25 26 // 跟微信平台比较的参数 27 String signature = request.getParameter("signature"); 28 String timestamp = request.getParameter("timestamp"); 29 String nonce = request.getParameter("nonce"); 30 String echostr = request.getParameter("echostr"); 31 32 PrintWriter pWriter = response.getWriter(); 33 // 微信get后进行比较 34 if (CheckUtil.checkSignature(signature, timestamp, nonce)) { 35 pWriter.print(echostr); 36 } 37 38 } 39 40 }
3、因为要一直连接,所以建立一个公共的util类,按照文章三中的步骤:
一、将token、timestamp、nonce三个参数进行字典排序
二、排序结束后,讲三个字符串拼接成一个字符串(token+timestamp+nonce),进行shar1加密.加密后会得到一个加密后的字符串
三、加密后的字符串与signature进行对比,如果相同,加密成功,如果不同,加密失败,如果成功,将echostr返回给微信,就是全部过程。
1 package com.my.utils; 2 3 import java.security.MessageDigest; 4 import java.util.Arrays; 5 6 //审核是否联通 7 public class CheckUtil { 8 9 // 公众号中的token 10 public static final String token = "aaaa"; 11 12 // 审核的方法 13 public static boolean checkSignature(String signature, String timestamp, 14 String nonce) { 15 // 将其装入一个数组 16 String[] arrStrings = new String[] { token, timestamp, nonce }; 17 18 // 排序 19 Arrays.sort(arrStrings); 20 21 // 生成字符串 22 StringBuffer sBuffer = new StringBuffer(); 23 for (int i = 0; i < arrStrings.length; i++) { 24 sBuffer.append(arrStrings[i]); 25 } 26 27 // sha1加密 28 String string = getShal(sBuffer.toString()); 29 return string.equals(signature); 30 31 } 32 33 // sha1加密算法 34 public static String getShal(String string) { 35 if (string == null || string.length() == 0) { 36 return null; 37 } 38 39 char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 40 'a', 'b', 'c', 'd', 'e', 'f' }; 41 42 try { 43 44 MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); 45 46 messageDigest.update(string.getBytes("UTF-8")); 47 48 byte[] md = messageDigest.digest(); 49 int j = md.length; 50 char buf[] = new char[j * 2]; 51 52 int k = 0; 53 for (int i = 0; i < j; i++) { 54 byte b0 = md[i]; 55 buf[k++] = hexDigits[b0 >>> 4 & 0xf]; 56 buf[k++] = hexDigits[b0 & 0xf]; 57 } 58 return new String(buf); 59 } catch (Exception e) { 60 return null; 61 } 62 63 } 64 65 }
4、别忘记配置web.xml文档
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>WeXInConnectDemo1</display-name> <servlet> <servlet-name>WeChatServlet</servlet-name> <servlet-class>com.my.servlet.WeXinServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WeChatServlet</servlet-name> <url-pattern>/wx.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
5、这样微信连接工作就做好了