啓用開發者模式的必備條件java
1)完善帳號信息,如公衆帳號的頭像,功能介紹,運營地區等信息。公衆平臺的設置模塊能夠看到公衆帳號的所有信息。git
2)請求校驗程序:啓用開發模式的過程當中會要求填寫接口配置信息,須要先完成請求校驗程序的開發。服務器
請求校驗程序的開發:參考微信開發者文檔中「新手介入」中的「接入指南」。微信
開發者文檔中說明在啓用開發模式時,須要填寫URl和token。微信開發
第二步在開發者文檔中說明了請求校驗的做用和流程。咱們開發的請求校驗程序必須可以處理Http GET請求,而且要對請求者進行身份校驗,確保請求來自微信服務器。eclipse
請求校驗的流程:工具
(1)獲取HTTP GET請求的signature,timestamp,nonce和echostr參數加密
(2)將token,timestamp和nonce3個參數字典排序spa
(3)將排序後的3個參數按順序拼接成一個字符串,並對字符串進行sha-1加密code
(4)將sha-1加密後的字符串與參數signature進行對比,若是相等即請求來自微信服務器,須要原樣返回參數echostr。
核心代碼段:
CoreServlet.java
package org.weixin.servlet.CoreServlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.weixin.util.SignUtil; @WebServlet("/CoreServlet") /* * 核心請求處理類 * */ public class CoreServlet extends HttpServlet { private static final long serialVersionUID = 1L; public CoreServlet() { super(); } /* * 確認請求來自微信服務器 * */ 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(SignUtil.checkSignature(signature,timestamp,nonce)) { out.print(echostr); } out.close(); out=null; } /* * 處理微信服務器發來的消息 * */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
SignUtil類
package org.weixin.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; /* * 請求校驗工具類 */ public class SignUtil { private static String token="weixin2016"; /* * 驗證簽名 */ public static boolean checkSignature(String signature,String timestamp,String nonce) { String[] paramArr=new String[]{token,timestamp,nonce}; Arrays.sort(paramArr); String content=paramArr[0].concat(paramArr[1]).concat(paramArr[2]); String ciphertext=null; try{ MessageDigest md=MessageDigest.getInstance("SHA-1"); byte[] digest=md.digest(content.toString().getBytes()); ciphertext=byteToStr(digest); }catch(NoSuchAlgorithmException e){ e.printStackTrace(); } return ciphertext!=null?ciphertext.equals(signature.toUpperCase()):false; } private static String byteToStr(byte[] byteArray) { String strDigest=""; for(int i=0;i<byteArray.length;i++) { strDigest+=byteToHexStr(byteArray[i]); } return strDigest; } 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; } }
必要的公網環境,在這裏我使用的是新浪雲服務SAE,微博帳號登入。進入雲應用SAE
點擊建立新應用。說明一下SAE使用時須要付費,能夠查看其收費標準。填寫相應的項目信息。建立成功。在eclipse中導出已編寫的後臺接口核心程序(war格式導出)。
在SAE界面中的代碼管理中選擇上傳war包。