java:setting_truststore_and_keystore_in_runtime

Setting TrustStore and KeyStore in Runtime

Sometime we might not want to mess up the Java JRE/JDK trust store and key store while we are testing some crazy shit like self-signed cert. We can do that in our application during running. Here is the code to do it:

 System.setProperty("javax.net.ssl.trustStore", "myTrustStore.jks");
 System.setProperty("javax.net.ssl.trustStorePassword", "password");

 System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
 System.setProperty("javax.net.ssl.keyStore", "myKeyStore.p12");
 System.setProperty("javax.net.ssl.keyStorePassword", "password");

myTrustStore.jks is the trust store file that created from the CA public cert, and myKeyStore.p12 is the key store that created from your private key, public key, and the CA's public key. The password are set by you during the cert conversion. Both files are supposed to put int the root directory of your project since we do not put any path in front of the files' names.

To know more about how those cert files are converted into Java format, you can find it here: https://wiki.chongtin.com/tslcert/creating_cert.

  • java/setting_truststore_and_keystore_in_runtime.txt
  • Last modified: 2019/10/18 11:33
  • by chongtin