I have a java keystore with which I can connect to a protected https third-party service. I use this keystore explicitely in my code when I initialize my web client:
// Solution #1
String password = "changeit";
KeyStore keyStore = KeyStore.getInstance(new File("src/main/resources/keystore.jks"), password.toCharArray());
SSLContext sslContext = new SSLContextBuilder()
.loadKeyMaterial(keyStore, password.toCharArray())
.build();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, (hostname, session) -> true);
HttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.build();
With this approach, everything works fine.
But I also know that there is a possibility to specify the system variables javax.net.ssl.keyStore
and javax.net.ssl.keyStorePassword
. So I was expecting an alternative solution to the code above will also work:
// Solution #2
System.setProperty( "javax.net.ssl.keyStore", "src/main/resources/keystore.jks");
System.setProperty( "javax.net.ssl.keyStorePassword", "changeit");
HttpClient httpClient = HttpClients.createDefault();
where I create a default web client without constructing SSLContext with my keystore explicitly. I have expected that the default web client will take somehow the keystore automatically from javax.net.ssl.keyStore
. But it seems it did not take and this solution did not work for me.
So I wonder what is the purpose of the use of system property javax.net.ssl.keyStore
? How it can be useful? What is the best practice here?