【问题标题】:Java HttpsURLConnection SSLHandshakeExceptionJava HttpsURLConnection SSLHandshakeException
【发布时间】:2011-10-02 14:58:30
【问题描述】:

我正在尝试使用 HttpsURLConnection 与网站建立 HTTPS 连接,然后执行 PUT 请求。当我尝试从HttpsURLConnection.getOutputStream() 创建OutputStreamWriter 时,会抛出以下异常:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: 
unable to find valid certification path to requested target

该页面刚刚获得其证书(由 StartCom 颁发) - 我是否需要手动执行某些操作以使 Java 识别该证书现在存在?我可以毫无困难地连接到同一网站的其他页面,但它们的证书不同。

【问题讨论】:

    标签: java https http-put sslhandshakeexception


    【解决方案1】:

    您可以使用工具 keytool(来自 JDK)将 StartCom 的根证书导入 Java 密钥库(JKS),然后将密钥库设置为“受信任的存储”。

    请参阅“导出和导入证书”部分:

    http://java.sun.com/developer/technicalArticles/Security/secureinternet2/

    那篇文章中提到的命令:

    将证书导入trustedcerts.jks:

    keytool -import -keystore trustedcerts.jks -alias qusay -file server.crt
    

    使用自定义 tuststore 启动 Java:

    java -Djavax.net.ssl.trustStore=trustedcerts.jks com.example.MyClass
    

    或者,您可以在运行时设置信任库:

    System.setProperty("javax.net.ssl.trustStore","./trustedcerts.jks");
    

    【讨论】:

    • 谢谢 - 对于有同样问题的其他人,我将证书保存在本地,使用 keytool -import ... 命令并将 System.setProperty("javax.net.ssl.trustStore", "path_to_keystore"); 添加到我的代码中。
    【解决方案2】:

    也请看这个问题:Import StartCom CA certificates in Windows JRE

    它链接到一个脚本,用于将 StartCom 证书导入您的 JDK 可信存储。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-05
      • 2021-11-15
      • 2015-05-16
      • 2010-11-17
      • 2016-11-05
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多