一、Sunny-Ngrok简单介绍
国内官网地址:https://www.ngrok.cc/
第一步:首先注册账户
使用邮箱进行注册即可。
第二步:进行开通隧道
选择:香港Ngrok免费服务器,开通连接:https://www.ngrok.cc/user.html
详细参数填写参照原文:https://www.sunnyos.com/article-show-67.html
第三步:下载客户端
选择合适的系统版本,进行下载
下载连接:https://www.ngrok.cc/download.html
第四步:启动隧道
启动教程:http://www.ngrok.cc/_book/start/ngrok_windows.html
隧道ID见开通隧道界面的隧道管理菜单栏。
二、微信公众号:服务器配置遇到的问题
1. 请求url超时
修改办法:最后更换为Ngrok后,提交成功了;
百度网盘下载链接:
链接:https://pan.baidu.com/s/1JL_iUSwp_qOD-F-D5dn7zg
提取码:rtkz
复制这段内容后打开百度网盘手机App,操作更方便哦
下载客户端后,解压,切换到ngrok.exe所在目录(如E:\ngrok-stable-windows-amd64),打开cmd命令窗口
输入命令:ngrok http 8080
得到如下的如下界面:
2. 服务器配置校验的代码
WeixinServlet.java
package com.imooc.study;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeiXinServlet extends HttpServlet {
@Override
protected 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");
PrintWriter out = response.getWriter();
if(CheckUtils.checkSignature(signature,timestamp,nonce)){
out.print(echostr);
}
}
}
CheckUtils.java
package com.imooc.study;
import java.security.MessageDigest;
import java.util.Arrays;
public class CheckUtils {
public static final String token = "yanluo";//申请公众号时自己输入
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 shaContent = "";
try {
shaContent = shaEncode(content.toString());
} catch (Exception e) {
e.printStackTrace();
}
return shaContent.equals(signature);
}
//sha1加密算法
public static String shaEncode(String inStr) throws Exception {
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
byte[] byteArray = inStr.getBytes("UTF-8");
byte[] md5Bytes = sha.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
public static void main(String args[]) throws Exception {
String str = new String("amigoxiexiexingxing");
System.out.println("原始:" + str);
System.out.println("SHA后:" + shaEncode(str));
}
}