Could not find a valid certification path for the requested target - java

I am trying to connect to a website using an HttpClient object. It works great for the sites we usually use (e.g. Google). But there is a website, when I try to connect, my program gives this error.

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 access this URL from a browser, I need to click continue anyway . Otherwise, the browser will not load the page. It gives a confidential error saying that your connection is not private .

How can I solve this problem in my java application ..? I want my software to contact this URL without any errors or without any acknowledgment.

+6
source share
2 answers

The problem was resolved when I used the TrustSelfSignedStrategy object as Trust material for HttpClient ..

  httpClient = HttpClients.custom() .setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom() .loadTrustMaterial(null, new TrustSelfSignedStrategy()) .build() ) ).build(); 

The code I use is shown above.

+9
source

For HttpClient4.x, the following will trust everyone

 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); } 
+2
source

Source: https://habr.com/ru/post/986659/


All Articles