1. 程式人生 > >微信公眾平臺開發2-接入指南(驗證伺服器地址有效性)

微信公眾平臺開發2-接入指南(驗證伺服器地址有效性)

接入指南 
一、填寫伺服器配置 
在測試號管理中填寫介面配置資訊 

伺服器地址URL,URL是開發者用來接收微信訊息和事件的介面URL; 
Token可以任意填寫; 
這是測試時填寫的,真實專案的填寫還要填寫EncodingAESKey,可以由開發者手動填寫或隨機生成,將用作訊息體加解密金鑰; 
開發者還可選擇訊息加解密方式:明文模式、相容模式和安全模式; 
具體可參看開發者文件。 
二、驗證伺服器地址的有效性 
開發者提交資訊後,微信伺服器將傳送GET請求到填寫的伺服器地址URL上,GET請求攜帶四個引數

通過檢驗signature對請求進行校驗。若確認此次GET請求來自微信伺服器,請原樣返回echostr引數內容,則接入生效,成為開發者成功,否則接入失敗

加密/校驗流程如下:

1. 將token、timestamp、nonce三個引數進行字典序排序

2. 將三個引數字串拼接成一個字串進行sha1加密

3. 開發者獲得加密後的字串可與signature對比,標識該請求來源於微信

示例程式碼如下:

public class WebChatCallBackServlet 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();  
        // 通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗  
        if (SignatureUtil.checkSignature(signature, timestamp, nonce)) {  
            out.print(echostr);  
        }  
        out.close();  
        out = null;  
    }
    /**
     * 處理微信發來的請求
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }
}

工具類:

/**
 * 驗證Signature工具類
 * @author 洋
 *
 */
public class SignatureUtil {

    // 與介面配置資訊中的Token要一致  
    private static String token = "weixin101";  

    /** 
     * 驗證簽名 
     *  
     * @param signature 
     * @param timestamp 
     * @param nonce 
     * @return 
     */  
    public static boolean checkSignature(String signature, String timestamp, String nonce) { 
        boolean isEqual=false;
        String[] arr = new String[] { token, timestamp, nonce };  
        // 將token、timestamp、nonce三個引數進行字典序排序  
        Arrays.sort(arr);  

        //三個引數組合成一個字串
        StringBuilder content = new StringBuilder();  
        for (int i = 0; i < arr.length; i++) {  
            content.append(arr[i]);  
        }  

        String tmpStr = null;  
        //進行SHA1加密,返回16進位制字串
        tmpStr=encryptSHA1(content.toString());

        // 將sha1加密後的字串可與signature對比,標識該請求來源於微信
        if(tmpStr!=null&&tmpStr.equals(signature)){
            isEqual=true;
        }else{
            isEqual=false;
        }
        content = null;  

        return isEqual;  
    }  
    /**
     * SHA1加密
     * 
     * @param data
     * @return
     */
    public static String encryptSHA1(String data){
        String str=null;
        try {
            // 獲得SHA1摘要演算法的 MessageDigest 物件
            MessageDigest md=MessageDigest.getInstance("SHA-1");
            // 使用指定的位元組更新摘要
            md.update(data.getBytes());
            //獲得密文
            byte[] bytes=md.digest();
            //位元組陣列轉化為16進位制字串
            str=bytesToHexString(bytes);

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
    /**
     * 位元組陣列轉化為16進位制字串
     * @param bytes
     * @return
     */
    public static String bytesToHexString(byte[] bytes){
        StringBuffer sb=new StringBuffer();
        if(bytes==null||bytes.length<=0){
            return null;
        }
        for(int i=0;i<bytes.length;i++){
            int temp=bytes[i]&0xFF;//與運算,將byte轉化為整型
            String hex=Integer.toHexString(temp);//int型轉化為16進位制字串
            if(hex.length()<2){
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }  

}

三、根據介面文件實現業務邏輯 
驗證URL有效性成功後即接入生效。 
現在你就可以根據微信介面文件進行開發了
----------------------------------------------------------------------------- 
作者:ChronosLiu 
來源:CSDN 
原文:https://blog.csdn.net/ly20116/article/details/51079140 
版權宣告:本文為博主原創文章,轉載請附上博文連結!