当我创建这样的SSL套接字时:
sslSocket = (SSLSocket) socketFactory.createSocket(host, port);一切都正常。socketFactory是SSLSocketFactory.getDefault()。但是,我想设置连接超时。因此,我将上面的代码更改为以下代码:
sslSocket = (SSLSocket) socketFactory.createSocket();
sslSocket.connect(new InetSocketAddress(host, port), connectionTimeout);但我得到了以下例外:
Exception in thread "main" jodd.http.HttpException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target;原因例外:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target在调试时,我注意到在第一种情况下,代码从请求的站点获取所有有效的证书。在第二种情况下,这些证书丢失了,因此出现了错误。
我还需要在我的2行更改中添加什么,才能使它像第一个一线版本一样工作呢?
完全STACKTRACE
jodd.http.HttpException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; <--- sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at jodd.http.HttpRequest.open(HttpRequest.java:667)
at jodd.http.HttpRequest.open(HttpRequest.java:649)
at jodd.http.HttpRequest._send(HttpRequest.java:747)
at jodd.http.HttpRequest.send(HttpRequest.java:742)
at jodd.JoddHttpTest.main(JoddHttpTest.java:56)
Caused by: 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:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at jodd.http.net.SocketHttpConnectionProvider.createSSLSocket(SocketHttpConnectionProvider.java:153)
at jodd.http.net.SocketHttpConnectionProvider.createHttpConnection(SocketHttpConnectionProvider.java:68)
at jodd.http.HttpRequest.open(HttpRequest.java:665)
... 4 more
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:1491)
... 14 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:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 20 more
---[cause]------------------------------------------------------------------------
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
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:1491)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at jodd.http.net.SocketHttpConnectionProvider.createSSLSocket(SocketHttpConnectionProvider.java:153)
at jodd.http.net.SocketHttpConnectionProvider.createHttpConnection(SocketHttpConnectionProvider.java:68)
at jodd.http.HttpRequest.open(HttpRequest.java:665)
at jodd.http.HttpRequest.open(HttpRequest.java:649)
at jodd.http.HttpRequest._send(HttpRequest.java:747)
at jodd.http.HttpRequest.send(HttpRequest.java:742)
at jodd.JoddHttpTest.main(JoddHttpTest.java:56)发布于 2016-08-03 11:55:30
这是答案。根据我的经验,这:
sslSocket = (SSLSocket) socketFactory.createSocket();
sslSocket.connect(new InetSocketAddress(host, port), connectionTimeout);不起作用(尽管你可以在任何地方看到这个解决方案!)。相反,我执行了以下操作:创建一个常规套接字,然后将其包装为SSL套接字:
Socket sock = new Socket();
sock.connect(new InetSocketAddress(host, port), connectionTimeout);
sslSocket = (SSLSocket)socketFactory.createSocket(sock, host, port, true);其中socketFactory是一个SSLSocketFactory实例。
https://stackoverflow.com/questions/38740399
复制相似问题