【问题标题】:Setting multiple truststore on the same JVM在同一个 JVM 上设置多个信任库
【发布时间】:2011-11-27 07:45:41
【问题描述】:

我有一个在 weblogic 服务器上运行的 Java 应用程序。该应用程序有两个不同的模块,它们使用 SSL 连接到外部 Web 服务 - 比如说模块 A 和模块 B。

模块 A - 建立在 Axis 上 - 使用信任库 A Moudle B - 基于 Spring-ws 构建 - 使用信任库 B。

模块 A 已存在。正在引入模块 B。

我需要能够根据调用的模块在 JVM 中动态设置信任库。

由于一些限制,我没有选择 - 创建自定义密钥管理器。 - 使用一个信任库

我尝试使用 System.setProperty im Module B 代码库来设置信任库。然而,它只有在模块 B 被首先调用时才有效。例如 - 说 我重新启动了 JVM 然后我调用模块 A - 它在 JVM 中设置了它自己的信任库 然后我调用模块 B - 它失败了 - 即使我使用了 System.setProperty 方法,它也没有在 JVM 中设置它自己的信任库。

我是否遗漏了什么,或者只是 System.setProperty 没有覆盖现有的设置值。如果是这样,我的选择是什么。

【问题讨论】:

  • 为什么需要两个信任库?信任库仅告诉您可以信任哪些 CA 来验证对等方。为什么每个模块会有所不同?
  • 请看看这个,它可能对你有帮助:stackoverflow.com/questions/1793979/…

标签: java ssl keystore truststore jsse


【解决方案1】:

您可以在运行时动态加载受信任的密钥库。

// 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);

小心,因为SSLContext.getDefault() 会给你返回默认你不能修改的上下文,所以你必须创建一个新的,初始化它然后设置它new 上下文作为默认值。

最重要的是,如果您愿意,可以使用任意数量的信任库。

【讨论】:

    猜你喜欢
    • 2015-03-21
    • 2018-05-18
    • 1970-01-01
    • 2015-07-26
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    相关资源
    最近更新 更多