1. 程式人生 > 實用技巧 >微信公眾號開發:伺服器配置

微信公眾號開發:伺服器配置

由於專案需要微信公眾號的開發,弄了老半天,發現也不是那麼難弄。

對於微信公眾號開發,首先要有開發者許可權然後進行基本的配置。

登入進微信公眾號平臺,首頁最下面有個基本配置:

進入基本配置後,會看到兩個欄目,一個是公眾號開發資訊,一個是伺服器配置。

這裡主要講如何進行伺服器配置,公眾號開發資訊配置比較簡單,就自己去操

因為我已經配置過了伺服器,所以頁面有點不同,點選修改配置。

接下來按照圖裡面步驟

因為伺服器需要公網可以訪問的域名並且要80和443埠,可以使用nat對映軟體進行內網穿透(花生殼不能固定80埠,已經不能使用)

然後進行伺服器校驗

看一下官方文件:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319

我摘取比較重要的資訊:

開發者提交資訊後,微信伺服器將傳送GET請求到填寫的伺服器地址URL上,GET請求攜帶引數如下表所示:

引數描述
signature 微信加密簽名,signature結合了開發者填寫的token引數和請求中的timestamp引數、nonce引數。
timestamp 時間戳
nonce 隨機數
echostr 隨機字串

開發者通過檢驗signature對請求進行校驗(下面有校驗方式)。若確認此次GET請求來自微信伺服器,請原樣返回echostr引數內容,則接入生效,成為開發者成功,否則接入失敗。加密/校驗流程如下:

1)將token、timestamp、nonce三個引數進行字典序排序 2)將三個引數字串拼接成一個字串進行sha1加密 3)開發者獲得加密後的字串可與signature對比,標識該請求來源於微信

意思是說:對微信伺服器發過來訊息忠token、timestamp、nonce三個引數進行加密處理,然後加密得到的字串與signature微信加密簽名相比較,如果相等則返回echostr隨機字串。

先下載微信官方的示例:https://wximg.gtimg.com/shake_tv/mpwiki/cryptoDemo.zip

下面進行伺服器環境搭建:

在剛才下載的微信官方的示例將

SampleCode\SampleCode\Java\src\com\qq\weixin\mp\aes\SHA1.java該類與其依賴類匯入專案中,為方便操作可以直接將SampleCode\Java\src下的com包匯入進去,後面根據哪些不需要用到的類刪除。

並且將SHA1類改為public,(有可能匯入會是中文亂碼,重新複製貼上文字進去即可)

新建一個servlet類:

import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

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.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.SHA1;


/**
 * Servlet implementation class test
 */
@WebServlet("/test")
public class test extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public test() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        String signature=request.getParameter("signature");
        String timestamp=request.getParameter("timestamp");
        String nonce=request.getParameter("nonce");
        String echostr=request.getParameter("echostr");
        String token="jylife";
        String jiami="";
         try {
             jiami=SHA1.getSHA1(token, timestamp, nonce,"");//這裡是對三個引數進行加密
        } catch (AesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         System.out.println("加密"+jiami);
         System.out.println("本身"+signature);
            PrintWriter out=response.getWriter();
            if(jiami.equals(signature))
            out.print(echostr);
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

因為官方給的示例,SHA1類中getSHA1方法有四個引數token, timestamp, nonce, encrypt,但是我們只有三個引數,根據觀察程式碼可知,將第四個引數設定為“”不會影響操作。

所以在呼叫getSHA1方法時,傳入引數是token, timestamp, nonce,""

最後將加密的字串與signature進行比較,如果相等就放回echostr。

伺服器環境即搭建完畢。

點選提交即可,顯示提交成功即可

這裡需要強調一下,eclipse在自動建立servlet時會給get方法加上

response.getWriter().append("Served at: ").append(request.getContextPath());

這一句一定要刪除,否則會一直驗證失敗。