1.新建一个web project,我取的项目名称是weixin,使用8080端口,新建一个WeiXinServlet,web.xml如下
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <servlet-name>WeiXinServlet</servlet-name> <servlet-class>com.gf.web.controller.WeiXinServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WeiXinServlet</servlet-name> <url-pattern>/weiXinServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
项目运行之后,我们就可以通过http://localhost:8080/weixin/weiXinServlet访问该servlet
2.下载ngrok,下载之后双击打开,在命令窗口输入ngrok http 8080回车
我马赛克注释掉的就是映射的外网访问地址,和后面的localhsot:8080访问一样。
http://注释掉/weixin/weiXinServlet和http://localhost:8080/weixin/weiXinServlet访问的是刚才新建的WeiXinServlet
3.登录微信公众号,我是申请的测试账号,左侧菜单“开发”下“基本配置”,配置如下
4.WeiXinServlet代码如下
package com.gf.web.controller; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WeiXinServlet extends HttpServlet { private static String token = "pangpang"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); System.out.println(signature); // System.out.println(timestamp); // System.out.println(nonce); // System.out.println(echostr); if(checkSignature(signature, timestamp, nonce)){ response.getWriter().print(echostr); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public static boolean checkSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { token, timestamp, nonce }; // 排序 Arrays.sort(arr); // 生成字符串 StringBuffer content = new StringBuffer(); for (int i = 0; i < arr.length; i++) { content.append(arr[i]); } // sha1加密 String temp = getSha1(content.toString()); System.out.println(temp); return temp.equals(signature); } public static String getSha1(String str) { if (null == str || 0 == str.length()) { return null; } char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); mdTemp.update(str.getBytes("UTF-8")); byte[] md = mdTemp.digest(); int j = md.length; char[] buf = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; buf[k++] = hexDigits[byte0 & 0xf]; } return new String(buf); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } }5.提交第三步的配置,bingo。