HttpsURLConnection:使用IDE JDK可以进行身份 验证,但不能用于JRE!太奇怪了

问题描述 投票:0回答:1

我有一个使用HttpsURLConnection和pfx文件以及会话cookie来检索网页信息的应用程序。

问题是,如果我像这样启动罐子,那是可行的:

"C:\Program Files\Java\jdk1.8.0_111\bin\java.exe" -jar "app"

但是如果我用我的JRE启动jar,则不起作用:

 java -jar "app.jar"

只是没有被钉住...

我读到一些有关的内容:How to provide ntlm authentication while calling any url?

但是禁用此JRE“ NTLM”限制并没有帮助我。据我所知,我不使用“ NTLM”。

谢谢你...

   public void connect() {

    try {
        URL url = new URL(HTTPS_URL);
        conn = (HttpsURLConnection) url.openConnection();
        conn.setSSLSocketFactory(getFactory(new File(KEY_STORE_FILE), KEY_STORE_PASS, new File(TRUST_STORE_FILE), TRUST_STORE_PASS));
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Accept-Language", "es,es-ES;q=0.9,en;q=0.8,de;q=0.7");
        conn.setInstanceFollowRedirects(true);

        if (!cookies.isEmpty()) {
            for (Map.Entry<String, String> entry : cookies.entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    conn.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
        }

        conn.connect();

        this.headers = conn.getHeaderFields();

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;

        while ((line = br.readLine()) != null) {
            responsebody += line;
        }

        br.close();

    } catch (MalformedURLException e) {
        System.out.println(e.toString());
        e.printStackTrace();
    } catch (SSLPeerUnverifiedException e) {
        System.out.println(e.toString());
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }

}


    private SSLSocketFactory getFactory(File pKeyFile, String pKeyPassword, File pTrustStoreFile, String pTrustStorePassword) {

    SSLSocketFactory socketFactory = null;

    try {
        KeyManagerFactory keyManagerFactory;
        keyManagerFactory = KeyManagerFactory.getInstance(KEY_MANAGER_ALGORITHM);
        KeyStore keyStore;
        keyStore = KeyStore.getInstance(KEY_STORE_FORMAT);
        InputStream keyInput = new FileInputStream(pKeyFile);

        keyStore.load(keyInput, pKeyPassword.toCharArray());
        keyInput.close();
        keyManagerFactory.init(keyStore, pKeyPassword.toCharArray());

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TRUST_MANAGER_ALGORITHM);
        KeyStore trustStore;
        trustStore = KeyStore.getInstance(TRUST_STORE_FORMAT);
        InputStream trustStoreInput = new FileInputStream(pTrustStoreFile);
        trustStore.load(trustStoreInput, pTrustStorePassword.toCharArray());
        trustStoreInput.close();
        trustManagerFactory.init(trustStore);

        SSLContext context = SSLContext.getInstance(SSL_CONTEXT_ALGORITHM);
        context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());
        socketFactory = context.getSocketFactory();

    } catch (IOException e) {
        if (e.toString().contains("keystore password was incorrect")) {
            System.out.println("Contraseña del certificado inválida: " + pKeyFile.getName());
        }
        System.out.println(e.toString());
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
        return (socketFactory);
    }
    return socketFactory;
}
java https httpsurlconnection
1个回答
1
投票

我解决了,如果可以帮助别人,我会问自己:

问题是我用了以下句子:

conn.setInstanceFollowRedirects(true);

并且远程主机将重定向到具有不同IP地址的子域。因此,在JDK的较新版本中,他们添加了一个限制,即您不能重定向到其他ip地址...它将清除您的所有请求数据。这就是为什么在JDK版本中我的应用程序可以工作,而在较新版本中它却无法工作。

我解决不带.instanceFollowRedirects的重定向的解决方法:

    Map<String,List<String>> headers = conn.getHeaderFields();
    if (headers.containsKey("Location")){
        return cert_request(headers.get("Location").get(0),KEY_STORE_FILE,KEY_STORE_PASS,TRUST_STORE_FILE,TRUST_STORE_PASS,cookie);
    }

基本上,我返回带有新URL和上一个请求的参数的新请求对象,您可以根据需要进行操作。

© www.soinside.com 2019 - 2024. All rights reserved.