连接到URL时出错 – 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信息

有关网站证书的Chrome信息

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

谢谢。

该网站不提供完成证书链所需的中间证书 。 一些用户代理/浏览器具有称为AIA追逐的function,在那里他们下载必要的中间产品,但Java客户端不是其中之一。

SSL Labs resport显示不完整的证书链

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

与此同时,如果您想在客户端中解决此问题,可以下载缺少的中间证书并将其添加到Java证书库中 。

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

  • 打开chrome并提供URL。 在地址栏中
  • 单击带锁定图标的安全
  • 将出现带有一些选项的弹出窗口。
  • 选择证书 – > 有效链接。
  • 单击详细信息选项卡,然后选择复制到文件
  • 给出一些名字并保存。

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

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

现在我们需要安装证书

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

密钥库的默认密码是changeit

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

如果你的jre在jdk之外,那么提供它的路径。

  • keytool -import -file“C:\ Certificate \ c6.cer”-keystore“C:\ Program Files \ Java \ jre1.8.0_181 \ lib \ security \ cacerts”-alias“c66”-storepass changeit -noprompt