Java URL 连接 javax.net.ssl.SSLHandShakeException

Java URL Connection javax.net.ssl.SSLHandShakeException

已解决 - 我通过将我的 cacerts 文件从我的 windows 机器复制到 pi 来解决它。

我正在编写一个应用程序以使用长轮询从网络服务器获取 json 字符串,该应用程序 运行 在我开发的 windows 机器上运行良好。然后我将源代码移动到 raspberry pi 并编译它,现在它抛出这个异常:

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:1917)
            at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:301)
            at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:295)
            at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1369)
            at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:156)
            at sun.security.ssl.Handshaker.processLoop(Handshaker.java:925)
            at sun.security.ssl.Handshaker.process_record(Handshaker.java:860)
            at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)
            at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
            at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
            at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
            at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
            at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
            at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
            at VideoBoard.main(VideoBoard.java:29)
    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(PKIXValidator.java:387)
            at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
            at sun.security.validator.Validator.validate(Validator.java:260)
            at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
            at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
            at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
            at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1351)
            ... 13 more
    Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
            at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
            at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
            at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
            at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
            ... 19 more

我的网站由 GoDaddy 托管,他们也提供 SSL 证书。通过浏览器或 java 从我的桌面访问站点时没有任何问题,所有迹象表明 SSL 证书有效且受我的机器信任。

您在 Raspberry Pi 上使用的 OS 在您在服务器上使用的证书路径中没有受信任的证书。换句话说,Pi 不知道要相信你的服务器就是它所说的那样。

要解决此问题,您需要在您的 Pi 上安装 GoDaddy 的根证书。如果您使用 Raspbian 或其他 Debian 派生的 OS,here is a good write up on how to do this, and here 是 GoDaddy 的证书存储库。