【问题标题】:HttpsUrlConnection using KeyStore instead of TrustStore with WebSphere Liberty ProfileHttpsUrlConnection 使用 KeyStore 而不是使用 WebSphere Liberty Profile 的 TrustStore
【发布时间】:2016-09-19 17:16:30
【问题描述】:

我们在 WebSphere Liberty Profile 的 WebSphere TAI 中使用 HttpsUrlConnection 来连接到安全服务器。我在 SSL 证书错误方面遇到了很多问题,直到我发现它正在 WLP 密钥库中寻找签名者证书,而不是 WLP 信任库或 JVM 信任库。代码中没有设置这个,它必须是默认值。但我很困惑,因为当我们在其他代码中使用 HTTP 客户端时,它使用的是 JVM 的信任库。

如何让 HttpsUrlConnection 使用 WLP 或 JVM 信任库,而不是密钥库?

【问题讨论】:

  • 你的server.xml 是什么?在<ssl> 元素中,您可以拥有trustStoreRef="defaultTrustStore",它将指向配置的信任库。如果您忽略它,则默认信任库是密钥库。有关详细信息,请参阅Liberty:SSL configuration attributes
  • 谢谢。我已经在我的“server.xml”中设置了 SSL。我最终做了下面第一个答案中描述的事情。

标签: java ssl websphere-liberty


【解决方案1】:

您可以如下加载您的信任存储并将其设置为 SSLContext,该 SSLContext 可以设置为 HttpsUrlConnection。由于这是我使用默认值的示例,您应该将它们替换为适当的算法、协议和信任库类型。

        try (FileInputStream truststoreFile = new FileInputStream("path/to/your/truststore.jks")) {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
            char[] trustorePassword = "<truststorePassword".toCharArray();
            truststore.load(truststoreFile, trustorePassword);
            trustManagerFactory.init(truststore);
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            KeyManager[] keyManagers = {};//if you have key managers;

            sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), new SecureRandom());

            URL httpsUrl = new URL("<your https url>");
            URLConnection urlConnection = httpsUrl.openConnection();

        } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e) {
            //handle exception
        } catch (KeyManagementException e) {
           //handle exception
        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
相关资源
最近更新 更多