In this tutorial, we will explore to consume restful web services hosted on https URL with SSL certificates.
We will use Sprint RestTemplate to consume the restful web services. It is very easy to consume the web services hosted on HTTP protocol. Challange is consuming the web services hosted on HTTPS with SSL certificates enabled. I encountered the following exception when accessing SSL hosted web services
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1917)
We will use SSLContext to skip SSL validation.
What is SSL Context?
SSL Context is a collection of ciphers, trusted certificates, TLS extensions and options, and protocol versions. It acts as a factory to store multiple connections with the same settings and those are put together in a context. SSL connections are then created based on this context.
Difference between SSL Context and SSL Session
SSL session represents an established SLL relation while SSL Context is required to establish an SSL session.
We will use an instance of javax.net.ssl.SSLContext to skip SSL certificate checking with RestTemplate.
Code
Conclusion
In this tutorial, we discussed the SSL Context and Java code i.e. SSLContext class to skip the SSL certificate checking using RestTemplate. Happy coding :)
We will use Sprint RestTemplate to consume the restful web services. It is very easy to consume the web services hosted on HTTP protocol. Challange is consuming the web services hosted on HTTPS with SSL certificates enabled. I encountered the following exception when accessing SSL hosted web services
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1917)
We will use SSLContext to skip SSL validation.
What is SSL Context?
SSL Context is a collection of ciphers, trusted certificates, TLS extensions and options, and protocol versions. It acts as a factory to store multiple connections with the same settings and those are put together in a context. SSL connections are then created based on this context.
Difference between SSL Context and SSL Session
SSL session represents an established SLL relation while SSL Context is required to establish an SSL session.
We will use an instance of javax.net.ssl.SSLContext to skip SSL certificate checking with RestTemplate.
Code
SSLContext sslContext = new SSLContextBuilder() .loadTrustMaterial(null, (certificate, authType) -> true).build(); CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext) .setSSLHostnameVerifier(new NoopHostnameVerifier()) .build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); RestTemplate restTemplate = new RestTemplate( requestFactory );
Conclusion
In this tutorial, we discussed the SSL Context and Java code i.e. SSLContext class to skip the SSL certificate checking using RestTemplate. Happy coding :)
Comments
Post a Comment