【问题标题】:Java SSLHandshakeException with self-signed certificate带有自签名证书的 Java SSLHandshakeException
【发布时间】:2016-04-29 13:27:10
【问题描述】:

我正在尝试使用自签名证书访问本地 https 站点。我修改了主机文件,并为我的本地站点分配了一个 IP 地址;我用来访问该站点的代码:

 String httpsURL = "https://test-ssl.com";
    URL myurl = new URL(httpsURL);
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
    InputStream ins = con.getInputStream();
    InputStreamReader isr = new InputStreamReader(ins);
    BufferedReader in = new BufferedReader(isr);

    String inputLine;

    while ((inputLine = in.readLine()) != null)
    {
      System.out.println(inputLine);
    }

    in.close();
  }

我收到此错误消息:

Exception in thread "main" 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
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)

我已使用以下命令将自签名证书导入密钥库:

keytool -import -alias site-ssl -keystore cacerts -file site-ssl.com.cer

证书导入成功

我在这里错过了什么?

【问题讨论】:

    标签: java ssl


    【解决方案1】:

    您应该尝试使用主机名而不是 URL 中的 IP 地址。它正在尝试进行主机名验证,并且证书不包含 192.168.1.6 的主题备用名称。

    【讨论】:

    • 修改了问题,可以看看吗?谢谢!
    • 您的 JVM 没有使用您将证书作为信任库导入的 cacerts 文件。
    • 我同意@EJP,这可能可行:keytool -import -alias site-ssl -keystore $JAVA_HOME/jre/lib/security/cacerts -file site-ssl.com.cer
    猜你喜欢
    • 2019-12-16
    • 2014-05-05
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2014-05-05
    相关资源
    最近更新 更多