【问题标题】:SSLHandshakeException when multiple ssl connections多个 ssl 连接时出现 SSLHandshakeException
【发布时间】:2012-04-16 03:14:38
【问题描述】:

我编写了一个模块,该模块通过 https 连接到具有身份验证的服务。设置正确的密钥库路径后,它工作正常。当我想在我的 Tomcat 应用程序中使用该模块(作为 jar)时出现问题。我也为密钥库设置了正确的路径(绝对路径),但是当我尝试连接时,我得到握手异常

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

我记得我之前收到了这条消息,当时我的密钥库不正确。我是否需要做更多的事情才能使它在 Tomcat 下工作。还有什么我错过的问题吗? 我在没有身份验证的情况下通过 https 连接到另一个服务,这工作正常(在 Tomcat 应用程序中)。

edit:问题是运行一个通过 ssl 连接到不同服务的项目(不仅在 Tomcat 中)。一个有身份验证,第二个没有。所以我编辑了标题

【问题讨论】:

  • 这个错误是在客户端还是在你的tomcat日志中?当您在客户端信任库中没有服务器证书(或其父证书之一)时,通常会在客户端看到此错误
  • 您在哪里以及如何配置您的信任库?
  • 你的答案似乎合适,你应该接受它(所以这个问题被标记为已解决)

标签: java authentication ssl sslhandshakeexception


【解决方案1】:

Setting multiple truststore on the same JVM 给了我答案。我只需要设置我的密钥工厂和信任工厂就可以了:)

System.setProperty 未设置已设置的 ssl 属性。

    // load your key store as a stream and initialize a KeyStore
    InputStream trustStream = new FileInputStream("Resources/keystore.ImportKey");
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());

    // if your store is password protected then declare it (it can be null however)
    String trustPassword = "changeit";

    // load the stream to your store
    trustStore.load(trustStream, trustPassword.toCharArray());

    // initialize a trust manager factory with the trusted store
    TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustFactory.init(trustStore);

    KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyFactory.init(trustStore, trustPassword.toCharArray());

    // get the trust managers from the factory
    TrustManager[] trustManagers = trustFactory.getTrustManagers();
    KeyManager[] keyManagers = keyFactory.getKeyManagers();

    // initialize an ssl context to use these managers and set as default
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(keyManagers, trustManagers, null);
    SSLContext.setDefault(sslContext);

工作正常!

【讨论】:

    【解决方案2】:

    我再一次回答自己。 阅读How to control SSLContext scope 后,我决定做同样的事情,而且效果很好。默认情况下,我没有设置任何 trustStore(因此使用默认的 cacerts),当我需要进行身份验证时,我使用

    httpsUrlConnection.setSSLSocketFactory(getSSLContext().getSocketFactory());
    

    当 getSSLContext() 返回我上面写的(没有 setDefault)时

    我想知道如何更改 Tomcat 应用程序中的默认 SSLContext,如果有人能提供帮助,我将不胜感激

    【讨论】:

    • 这不是一个论坛,而是一个问答网站。尝试对一个问题最多有一个答案。如果您尝试了新的东西,请编辑您的问题,而不是在您自己的问题的答案中添加更多详细信息。
    猜你喜欢
    • 2018-03-30
    • 2023-03-03
    • 2017-10-24
    • 2015-06-05
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多