Setting multiple truststore on the same JVM

后端 未结 1 434
孤独总比滥情好
孤独总比滥情好 2020-12-03 12:19

I have an Java application running on a weblogic server. The application has two distinct modules which use SSL to connect to external web services - let\'s say module A and

相关标签:
1条回答
  • 2020-12-03 13:05

    You can load trusted key stores dynamically at runtime.

    // load your key store as a stream and initialize a KeyStore
    InputStream trustStream = ...    
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());    
    
    // if your store is password protected then declare it (it can be null however)
    char[] trustPassword = ...
    
    // load the stream to your store
    trustStore.load(trustStream, trustPassword);
    
    // initialize a trust manager factory with the trusted store
    TrustManagerFactory trustFactory = 
      TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());    
    trustFactory.init(trustStore);
    
    // get the trust managers from the factory
    TrustManager[] trustManagers = trustFactory.getTrustManagers();
    
    // initialize an ssl context to use these managers and set as default
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustManagers, null);
    SSLContext.setDefault(sslContext);
    

    Watch out, because SSLContext.getDefault() would give you back the default context which you cannot modify, so you have to create a new one, initialize it then set this new context as the default.

    The bottom line is that you can use any number of trust stores if you want to.

    0 讨论(0)
提交回复
热议问题