java ssl connection using truststore

2019-02-26 21:12发布

问题:

I have a web application that connects to a https site. For that I have to install the certificate in the cacerts in lib/security folder. I would like to create a truststore and place it within the application and while connecting to the other site make the app refer to this truststore so that when the application is moved the certificates move with it.

The way I connect to the https site is using a form post. How do I go about doing this. Also, when doing a form post rather than using a HttpsURLConnection, how to configure to look into custom truststore instead of cacerts.

回答1:

The easiest way is to set a system property when launching the java process: -Djavax.net.ssl.trustStore=...

This is used by the default TrustManager, as described in http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html#X509TrustManager

Alternatively, override the TrustManager as described in the next paragraph in the same document.