无法找到请求目标的有效认证路径 - java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30078081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to find valid certification path to requested target - java
提问by Ramesh-X
I'm trying to connect to a website using a HttpClientobject. It works fine for websites we normally use(Like google). But there is a web site, when I try to connect, my program gives this error..
我正在尝试使用HttpClient对象连接到网站。它适用于我们通常使用的网站(如谷歌)。但是有一个网站,当我尝试连接时,我的程序给出了这个错误..
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)
....................
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)
...............
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)
... 27 more
When I try to go to this url from the browser, I have to click continue anyway. Otherwise browser will not load the page. It gives a privacy errorsaying your connection is not private.
当我尝试从浏览器转到此 url 时,无论如何我必须单击continue。否则浏览器不会加载页面。它给出了一个隐私错误,说您的连接不是私有的。
How can I overcome this problem in my java application..? I want my software to connect with that url without any error or without asking any confirmation.
我怎样才能在我的 Java 应用程序中克服这个问题..?我希望我的软件与该 url 连接而不会出现任何错误或不要求任何确认。
回答by Ramesh-X
Problem was solved when I used a TrustSelfSignedStrategyobject as the Trust material to HttpClient.
当我使用TrustSelfSignedStrategy对象作为HttpClient的 Trust 材料时,问题解决了。
httpClient = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build()
)
).build();
The code I used is shown above..
我使用的代码如上所示..
回答by greensuisse
For HttpClient4.x, the following will trust all
对于 HttpClient4.x,以下将信任所有
public static HttpClientBuilder createTrustAllHttpClientBuilder() {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, (chain, authType) -> true);
SSLConnectionSocketFactory sslsf = new
SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
return HttpClients.custom().setSSLSocketFactory(sslsf);
}