本文共 5592 字,大约阅读时间需要 18 分钟。
先决条件:
配置前的准备-内网穿透
由于项目搭建在本地电脑上,外网无法访问,所以需要使用工具将本地地址映射到公网。免费工具使用:natapp
注意:这里只能使用80端口,因为微信公众号只开放80端口使用
免费隧道配置
先注册,注册成功后登录。 隧道购买成功后,在我的隧道中就可以看到已拥有的隧道: 客户端下载 我们访问到natapp的客户端下载,下载natapp客户端: 下载后,解压,会有一个natapp.exe的文件。运行natapp
在运行natapp之前需要先配置,详细教程参考:。 config.ini内容: 注意:config.ini配置文件需要与natapp.exe在同一个目录下。连接
本地启动javaweb项目后:访问localhost:80
, 直接双击natapp.exe运行,访问自己的连接http://wge5gq.natappfree.cc/:80
注意:由于我这里使用的是springboot,默认端口为8080,所以需要修改配置文件application中的端口配置
关闭iis服务器:Internet Information Services
由于win10系统的iis占用80端口,所以导致配置失败,需要关闭 步骤:控制面板 -> 程序 -> 启动或关闭 Windows 功能 -> 取消 ‘Internet Information Services’ 选项 -> 确定请求验证
微信公众平台需要发送给服务器一个请求来确认服务器连接
这个需要在项目中添加单独的请求连接
几个参数
请求校验代码编写
package cn.ictgu.tools;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/** * @author: ZouTai * @date: 2018/5/3 * @description: */public class CheckoutUtil { // 与接口配置信息中的Token要一致 private static String token = "TOKEN"; /** * 验证签名 * * @param signature * @param timestamp * @param nonce * @return */ public static boolean checkSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { token, timestamp, nonce }; // 将token、timestamp、nonce三个参数进行字典序排序 // Arrays.sort(arr); 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; } /** * 将字节数组转换为十六进制字符串 * * @param byteArray * @return */ private static String byteToStr(byte[] byteArray) { String strDigest = ""; for (int i = 0; i < byteArray.length; i++) { strDigest += byteToHexStr(byteArray[i]); } return strDigest; } /** * 将字节转换为十六进制字符串 * * @param mByte * @return */ 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; } } } }}package cn.ictgu.tools;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/** * @author: ZouTai * @date: 2018/5/3 * @description: */public class CheckoutUtil { // 与接口配置信息中的Token要一致 private static String token = "TOKEN"; /** * 验证签名 * * @param signature * @param timestamp * @param nonce * @return */ public static boolean checkSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { token, timestamp, nonce }; // 将token、timestamp、nonce三个参数进行字典序排序 // Arrays.sort(arr); 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; } /** * 将字节数组转换为十六进制字符串 * * @param byteArray * @return */ private static String byteToStr(byte[] byteArray) { String strDigest = ""; for (int i = 0; i < byteArray.length; i++) { strDigest += byteToHexStr(byteArray[i]); } return strDigest; } /** * 将字节转换为十六进制字符串 * * @param mByte * @return */ 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; } } } }}
参考链接:
转载地址:http://qhepi.baihongyu.com/