连接到URL时出错 - PKIX路径构建失败

时间:2015-10-22 15:18:09

标签: java ssl encryption certificate pkix

我正在尝试连接到网页以收集信息,我正在使用jsoup来解析HTML。但是,每当我尝试连接到URL以下载源时,我都会收到有关PKIX构建路径的错误。 我环顾四周,我发现的所有内容都说将我网站的CA根证书添加到我的信任库,但问题仍然存在(CA根证书已经存在)。我可以通过Web浏览器连接到网站,但不能通过URL类连接到网站。这是我能编写的最基本的代码,它会产生错误。

public class URLConnectStart {
    public static void main(String[] args) {
        try {
            URL u = new URL("https://ntst.umd.edu/soc/");
            u.openStream();     
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是错误

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(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at URLConnectStart.run(URLConnectStart.java:14)
    at URLConnectStart.main(URLConnectStart.java:8)
Caused by: 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.validator.PKIXValidator.doBuild(Unknown Source)
    at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
    at sun.security.validator.Validator.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    ... 16 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
    ... 22 more

Chrome关于网站证书的信息

Info from chrome regarding the website's cert

任何帮助将不胜感激。这不是一个关键的应用程序,因此安全性并不是那么重要,但如果我能保持安全性,我宁愿这样做。无论如何,我希望能够做的就是通过代码下载本网站的HTML。

谢谢。

2 个答案:

答案 0 :(得分:3)

完成证书链所需的网站does not provide an intermediate certificate。一些用户代理/浏览器具有称为AIA的功能,他们下载必要的中间件,但Java客户端不是其中之一。

SSL Labs resport showing incomplete certificate chain

如果您是网站管理员,解决此问题的正确方法是提供中间证书,以便发送完整的链。即使您是最终用户,也请考虑与网站联系以解决此问题。由于此问题,使用Android浏览器的人也将无法访问此站点而未接受安全警告。

与此同时,如果您想在自己的客户中解决此问题,可以download the missing intermediate certadd it to your Java certificate store

答案 1 :(得分:0)

您可以手动将证书安装到Java密钥库中。

  • 打开chrome并提供URL。在地址栏中
  • 点击带有锁定图标的安全
  • 带有某些选项的弹出窗口将出现。
  • 选择位于其下方的证书-> 有效链接。
  • 点击详细信息标签,然后选择复制到文件
  • 命名并保存。

第一个清除是否存在别名的命令(我更喜欢在Windows中从JAVA_HOME运行此命令,因此路径是与此相对的。如果从外部运行,请相应地更改路径。

  • keytool-删除-alias“ c11” -keystore ../jre/lib/security/cacerts -storepass changeit -noprompt

现在我们需要安装证书

  • keytool-导入-file“ C:\ Certificateert \ c1.cer” -keystore ../jre/lib/security/cacerts -alias“ c11” -storepass changeit -noprompt
  • >

密钥库的默认密码为 changeit

如果证书数量很多,请将此命令添加到每个文本文件中,并将其另存为 bat 扩展名,并放置到 JAVA_HOME \ bin 或任何您喜欢的位置。确保提到的路径是相对的。

如果您的jre在jdk之外,请提供该路径。

  • keytool-导入-文件“ C:\ Certificate \ c6.cer”-密钥库“ C:\ Program Files \ Java \ jre1.8.0_181 \ lib \ security \ cacerts”-别名“ c66 “ -storepass changeit -noprompt
相关问题