微信公衆號開發接口指南(java實現)

我們進入微信公衆平臺查看開發者文檔,裏面有詳細的接入指南的步驟介紹,接下來我們逐步實現這個過程
1,首先我們要接受微信後臺傳到服務器上的四個參數,分別是signature(微信加密簽名),timestamp(時間戳),nonce(隨機數),echostr(隨機字符串),新建一個項目(wechatconnect),將這個步驟放到com.example.wechat包下,這個包下新建一個servlet,在doget方法中接受後臺傳遞過來的參數

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 com.zmt.weixin.CheckUtil;

/**
 * Servlet implementation class WeixinChat
 */
@WebServlet("/WeixinChat")
public class WeixinChat extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public WeixinChat() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String signature = request.getParameter("signature");//微信加密簽名
        String nonce = request.getParameter("nonce");//隨機數
        String echostr = request.getParameter("echostr");//隨機字符串
        String timestamp = request.getParameter("timestamp");//時間戳


        PrintWriter out = response.getWriter();
        if(CheckUtil.chatCheck(signature, timestamp, nonce)){
            out.println(echostr);
        }
    }





}

2,加密/校驗流程如下:
1. 將token、timestamp、nonce三個參數進行字典序排序
2. 將三個參數字符串拼接成一個字符串進行sha1加密
3. 開發者獲得加密後的字符串可與signature對比,標識該請求來源於微信
講個個步驟中的操作放到com.example.util包中,新建一個類weichatutil

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class CheckUtil {
    private static final String token = "123zmystatic123";

    public static boolean chatCheck(String signature, String timestamp, String nonce){
        String[] arr = new String[]{token, timestamp, nonce};
        Arrays.sort(arr);
        StringBuffer content = new StringBuffer();
        //練成字符串
        for (String string : arr) {
            content.append(string);
        }

        //sha1加密
        String tmp = SHA(content.toString());

        return tmp.equals(signature);

    }

    public static String SHA(String decript) {
        try {
            MessageDigest digest = java.security.MessageDigest
                    .getInstance("SHA");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字節數組轉換爲 十六進制 數
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}

3,最後要在web.xml文件中完成服務器加載的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>weixincon</display-name>


  <servlet>
    <servlet-name>weixin</servlet-name>
    <servlet-class>com.zmt.util.WeixinChat</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>weixin</servlet-name>
    <url-pattern>/weixin.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

接入指南就完成了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章