在同一个 JVM 上设置多个信任库

发布于 2024-12-07 03:05:31 字数 536 浏览 1 评论 0原文

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

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

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

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

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

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

我是否遗漏了某些内容,或者只是 System.setProperty 不会覆盖现有的设置值。如果是这样,我在这里有什么选择。

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 module B.

Module A - Built on Axis - Uses truststore A
Moudle B - Built on Spring-ws - Uses truststore B.

Module A is existing. Module B is being introduced.

I need to be able to set the truststore dynamically in the JVM based on which module is being invoked.

Due to some constraints I do not have the option
- to create a custom key manager.
- use one truststore

I tried to use System.setProperty im Module B codebase to set truststore. However it works only if Module B got invoked first. For example - Say
I have a fresh restart of the JVM
then I invoke module A - it set's it's own truststore in the JVM
then I invoke module B - It fails - it's does not set it's own truststore in the JVM even though I have used System.setProperty method.

Am I missing something or it's just that System.setProperty doesn't override existing set values. If so what are my options here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⊕婉儿 2024-12-14 03:05:31

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

// 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() 会返回您默认上下文,而您无法修改,因此您必须创建一个新上下文,初始化它,然后将此上下文设置为默认值。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文