Wednesday, August 28, 2013

Steps to enable SSL connection from Java to an https site

Follow these steps when you get an exception like one below when establishing URL connection to an https site:

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 

1. Open the site in any browser that support https and allow browser to accept a certificate from the site.

2. In browser preferences find a certificate manager and select tab with server certificates.

3. Find a certificate that belongs to the site and export it in X.509 certificate with chain (PEM) format

4. Run on Linux:

sudo keytool -keystore "$JAVA_HOME/jre/lib/security/cacerts" -alias CERTALIAS -storepass changeit -importcert -file CERTFILE.pem

or something like this on Windows:

keytool -keystore "C:/Program Files/Java/jdk1.6.0_20/jre/lib/security/cacerts" -alias CERTALIAS -storepass changeit -importcert -file CERTFILE.pem

Note: replace CERTALIAS with an alias for the certificate and CERTFILE with a file name you used to export certificate to.



Done.

No comments:

Post a Comment