微信公衆服務號接口接入,實現開發回覆功能,而非後臺設置

參考文檔地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319


接入概述

接入微信公衆平臺開發,開發者需要按照如下步驟完成:

1、填寫服務器配置

2、驗證服務器地址的有效性

3、依據接口文檔實現業務邏輯

  講到接口接入就不得不提,服務器實現公衆號自定義功能;因爲,接口接入是開發的基礎。

公衆號開發,需要1。申請公衆號,2.開發者認證 3,獲取公衆號開發信息 4.服務器配置啓動(接口接入)


這裏我只講下服務器配置(接口接入):




    private static final String TOKEN = "immco";
   

 /**
     * 微信接入模塊;接入微信公衆平臺開發第一步
     * @param request
     * @param response
     * @return
     * @throws ParseException
     * @author  lbh 
     * @throws IOException 
     * @date 創建時間:2018年1月18日 下午4:13:20  
     * @parameter
     */
    @RequestMapping(value = "/wxServerBridge", method = RequestMethod.GET)
    public void wxServerBridge(HttpServletRequest request,
            HttpServletResponse response)
            throws ParseException, 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 (WXAuthUtil.checkSignature(signature, timestamp, nonce)) {
        out.println(echostr);
       }
    }


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


        1)將token、timestamp、nonce三個參數進行字典序排序 2)將三個參數字符串拼接成一個字符串進行sha1加密 3)開發者獲得加密後的字符串可與signature對比,標識該請求來源於微信
     * */
    public static Boolean checkSignature(String signature,String timestamp ,String nonce){
        String[] arr = new String[]{TOKEN,timestamp,nonce};
        //1)將token、timestamp、nonce三個參數進行字典序排序
        Arrays.sort(arr);
        //2)將三個參數字符串拼接成一個字符串進行
        StringBuffer content = new StringBuffer();
        for(int i=0;i<arr.length;i++){
            content.append(arr[i]);
        }
        //3)sha1加密 
        String temp = getSha1(content.toString());
        String temp1=SHA1.encode(content.toString());
        System.out.println(signature);
        System.out.println(temp);
        System.out.println(temp1);
        //4)開發者獲得加密後的字符串可與signature對比,標識該請求來源於微信
        return temp1.equals(signature);
    }
    
    
   
    /**
     * sha1加密算法工具
     * @param str
     * @return
     * @author  lbh 
     * @date 創建時間:2018年1月18日 下午4:19:42  
     * @parameter
     */
    public static String getSha1(String str){
        if (null == str || 0 == str.length()){
            return null;
        }
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));
             
            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] buf = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }


代碼完成並部署可以訪問的外環境下,微信通過域名可以訪問到,這是就可以點擊“提交”;如果提示驗證通過就證明接口成功;接下來的所有公衆開發就可以逐步進行了。

  


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