위챗 애플릿이 되돌아오는 핸드폰 번호 분석 (권한 부여)

2235 단어 코드
String pMobile = body.getString("pMobile");
String ivdata = body.getString("ivdata");
String code = body.getString("code");
public static String decodeWxAppPhone(String encrypted, String iv, String code) {
        String wxAppHost = "https://api.weixin.qq.com";
        String wxAppPath =  "/sns/jscode2session" ;
        String wxAppId = "" ;
        String wxAppSecret = "";
        String path =
                wxAppPath
                        + "?appid="
                        + wxAppId
                        + "&secret="
                        + wxAppSecret
                        + "&js_code="
                        + code
                        + "&grant_type=authorization_code";
        try {
            //         get          

            String jsonStr = HttpUtil.doGet(wxAppHost+path,null);
//            HttpResponse response = HttpUtils.doGet(wxAppHost, path, "GET", null, null);
//            String jsonStr = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSON.parseObject(jsonStr);
            String sessionkey = jsonObject.getString("session_key");

            //   
            byte[] encrypData = Base64Utils.decodeFromString(encrypted);
            byte[] ivData = Base64Utils.decodeFromString(iv);
            byte[] sessionKey = Base64Utils.decodeFromString(sessionkey);
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivData);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(sessionKey, "AES");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

            String resultString = new String(cipher.doFinal(encrypData), "UTF-8");
            JSONObject object = JSONObject.parseObject(resultString);
            //       
            String phone = object.getString("phoneNumber");
            //       
            return phone;
        } catch (Exception e) {
            logger.error("             ,    :", e);
        }
        return  null;
    }

좋은 웹페이지 즐겨찾기