javahttpshttpsurlconnection

HttpsURLConnection: using IDE JDK works for authentication but not with JRE!! That's weird


I have an application that use HttpsURLConnection with an pfx file and session cookie to retrieve a webpage information.

The question is that is working if I launch the jar like this:

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

But it not works if I launch the jar with my JRE, with:

 java -jar "app.jar"

It just not get autenthicated...

I read some about this: How to provide ntlm authentication while calling any url?

But disabling this JRE "NTLM" restriction dont helped me. And as far I know im not using "NTLM".

Thank's you...

   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;
}

Solution

  • I solved it, I ask myself in case I can help someone:

    The problem were that I used the following sentence:

    conn.setInstanceFollowRedirects(true);
    

    And the remote host had redirections to a subdomain that have a different IP address. So in a newer version of JDK they added a restriction in that you can't have redirections to different ip addresses... It clears all your request data. That's why in a JDK version my app worked and in newer versions it doesn't worked.

    My workaround to follow redirects without .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);
        }
    

    Basically I return a new request object with the new URL and the parameters of the previous request, you can do it like you want.