一、准備環境
1、JDK1.6及以上版本
2、Eclipse
3、Tomcat
4、Ngrok
二、步驟
1、訪問微信公眾平台開發者手冊 https://mp.weixin.qq.com/wiki 如下是接入規則(來自開發者手冊):
開發者提交信息后,微信服務器將發送GET請求到填寫的服務器地址URL上,GET請求攜帶參數如下表所示:
| 參數 | 描述 |
| signature | 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。 |
| timestamp | 時間戳 |
| nonce | 隨機數 |
| echostr | 隨機字符串 |
開發者通過檢驗signature對請求進行校驗(下面有校驗方式)。若確認此次GET請求來自微信服務器,請原樣返回echostr參數內容,則接入生效,成為開發者成功,否則接入失敗。加密/校驗流程如下:
| 1)將token、timestamp、nonce三個參數進行字典序排序 2)將三個參數字符串拼接成一個字符串進行sha1加密 3)開發者獲得加密后的字符串可與signature對比,標識該請求來源於微信 |
2、接入微信開發者模式開始
我們細細品味微信提供的規則:若確認此次GET請求來自微信服務器,請原樣返回echostr參數內容,則接入生效,成為開發者成功,否則接入失敗
我們索性就在get方法中獲取echostr直接返回,按照此規則,我先來創建一個web項目,並創建一個Servlet,代碼如下:
package com.weixin.util;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SignUtil
*/
@WebServlet("/SignUtil")
public class SignUtil extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public SignUtil() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String echostr=request.getParameter("echostr"); response.getWriter().print(echostr);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
將項目不部署到Tomcat,可以正常訪問Servlet,返回null。OK
3、開啟Ngrok,用Ngrok映射的域名也可以正常訪問Servlet,若不懂Ngrok的設置,請參考我的博文 微信開發第一篇
4、登錄到微信公眾平台,在開發--》基本配置,首次開啟,會讓你確認,勾選“我同意”,成為開發者 如下圖:

5、在基本配置中,點擊修改配置,修改配置
URL:對應我們Servlet地址,注意:這里要是我們ngrok映射的域名
Token:可隨意的英文組合
EncodingAESKey:隨機生成
消息加密方式:明文模式

點擊【修改配置】

6、提交后點擊【啟用】,就可以啟用我們的開發者模式了


7、雖然我們投機取巧,成功的啟用開發者模式,但是這種方式是不符合微信的規則,
下面我們通過微信的方式,來實現啟用開發者模式:
1)將token、timestamp、nonce三個參數進行字典序排序
2)將三個參數字符串拼接成一個字符串進行sha1加密
3)開發者獲得加密后的字符串可與signature對比,標識該請求來源於微信
直接上代碼:
package com.weixin.util;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SignUtil
*/
@WebServlet("/SignUtil")
public class SignUtil extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String token = "weixin";
/**
* Default constructor.
*/
public SignUtil() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//微信服務器get傳遞的參數
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 (this.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}
out.close();
out = null;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
*
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public static boolean checkSignature(String signature, String timestamp, String nonce){
String[] arr = new String[]{token, timestamp, nonce};
//排序
Arrays.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");
//SHA-1加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
content = null;
// 比對 判斷
return tmpStr != null ? tmpStr.equals(signature.toUpperCase()): false;
}
/**
*
* @param digest
* @return
*/
private static String byteToStr(byte[] digest) {
// TODO Auto-generated method stub
String strDigest = "";
for(int i = 0; i < digest.length; i++){
strDigest += byteToHexStr(digest[i]);
}
return strDigest;
}
/**
*
* @param b
* @return
*/
private static String byteToHexStr(byte b) {
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[(b >>> 4) & 0X0F];
tempArr[1] = Digit[b & 0X0F];
String s = new String(tempArr);
return s;
}
}
8、重新啟用自己的開發者模式,看是否能成功,若驗證不通過,歡迎各位在評論區提問,謝謝各位
所有博文內容,全部是自己一步一步操作出來的,請尊重版權,若轉載請說明出處,謝謝。
