1. 程式人生 > >微信第三方平臺授權流程- java

微信第三方平臺授權流程- java

public void processAuthorizeEvent(HttpServletRequest request) {
        System.out.println("開始獲取微信推送資訊----------------------------");
        // 獲得微信推送的引數
        String nonce = request.getParameter("nonce");
        String timestamp = request.getParameter("timestamp");
        String signature = request.getParameter("signature");
        String msgSignature = request.getParameter("msg_signature");
        try {
            if (!StringUtils.isNotBlank(msgSignature)) {
                return;// 微信推送給第三方開放平臺的訊息一定是加過密的,無訊息加密無法解密訊息
            }
            String component_token = prop.getProperty("component_token");
            boolean isValid = checkSignature(component_token, signature, timestamp, nonce);
            if (isValid) {
                StringBuilder sb = new StringBuilder();
                BufferedReader in = request.getReader();
                String line;
                while ((line = in.readLine()) != null) {
                    sb.append(line);
                }
                // 微信推送的加密為資訊
                String xml = sb.toString();
                // 引數
                String component_encodingaeskey = prop.getProperty("component_encodingaeskey");
                String component_appid = prop.getProperty("weixinThirdPart_appid");
                // 加密資訊的解密工具類
                WXBizMsgCrypt pc = new WXBizMsgCrypt(component_token, component_encodingaeskey, component_appid);
                // 微信推送的加密資訊,解密後的xml
                xml = pc.decryptMsg(msgSignature, timestamp, nonce, xml);
                Document doc = DocumentHelper.parseText(xml);
                Element rootElt = doc.getRootElement();
                String type = rootElt.elementText("InfoType");
                if ("component_verify_ticket".equals(type)) {
                    processAuthorizationEvent(rootElt);
                } else if ("unauthorized".equals(type)) {
                    handleUnauthorized(rootElt);
                }
                System.out.println("type}}}}}}}}}}}}}}}" + type);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
解密的方法