How to get Remote server untrusted SSL certificate using Apache HTTP Client API

382 views Asked by At

I have a remote server which may or may not be running using a valid SSL cert (using self-signed SSL cert).

We are making connection to remote server, which may fail if remote server is using self-signed SSL cert. So, we want to be able to download/view the remote server cert if our SSL handshake fails.

If I use Apache HTTP Client then I couldn't find a method which could allow me to view remote server certificate (you can do it with HttpsURLConnection but we are trying to avoid using it see this example).

I also looked into Spring RestTemplate, and it didn't provide any option either - I searched on Google and didn't find anything around Spring or Apache HTTP Client.

1

There are 1 answers

0
ok2c On

This should give you pretty much a complete control over the process of trust verification.

SSLContext sslContext = SSLContextBuilder.create()
        .loadTrustMaterial((chain, authType) -> {
            for (X509Certificate cert: chain) {
                System.out.println(cert.getSubjectDN());
            }
            // Let the standard trust managers decide
            // whether or not the cert chain is trusted
            return false;
        })
        .build();

CloseableHttpClient client = HttpClientBuilder.create()
        .setSSLContext(sslContext)
        .build();