How to By Pass SSL Certificate in RestTemplate SpringBoot.?

I am getting below error:

I/O error on POST request for \"https://VRT-IP/router/engine/v1/gatewayProcessor\": PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

How we can resolve this issue?

2 Likes

We can use below method to call Api with Bypassing SSL Certificate.


 public <T> ResponseEntity<T> performRestIntegrationToTargetServer(HttpEntity<?> httpEntity, String endPointUrl, HttpMethod httpMethod, Class<T> clazz) {  
        try {   
            SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()     
                    .loadTrustMaterial(null, (chain, authType) -> true).build();     
            HttpClientBuilder clientBuilder = HttpClientBuilder.create();     
            clientBuilder.setSSLContext(sslContext);      
            clientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);  
            CloseableHttpClient httpClient = clientBuilder.build();
            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();    
            requestFactory.setHttpClient(httpClient);    
            RestTemplate restTemplate = new RestTemplate(requestFactory);   
            return restTemplate.exchange(endPointUrl, httpMethod, httpEntity, clazz);  
        } catch (HttpServerErrorException | HttpClientErrorException e) { 
            log.error("Error Response Body " + e.getResponseBodyAsString());    
            log.error("Error Status " + e.getStatusText());      
            log.error("Error in REST INTEGRATION ", e); 
        } 
        catch (Exception e) {      
            log.error("Error in REST INTEGRATION ", e);
        }
        
        return null;
    }
2 Likes

@naveen.gupta please accept if it resolves your query.