0

I have one requirement i.e. to bypass SSL verification while connecting to webservice using rest template.

Currently I am implementing proxy to rest template via below code.

    SimpleClientHttpRequestFactory clientHttpRequestFactory = new 
    SimpleClientHttpRequestFactory();
                       
    Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyHost, 
                                 Integer.parseInt(proxyPort)));
   clientHttpRequestFactory.setProxy(proxy);

   RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);

   ResponseEntity<String>  responseEntity= restTemplate.exchange(url,HttpMethod.POST, 
   entity,String.class);
   response = responseEntity.getBody();

How can I modify my Rest template so while connecting to URL it doesn't look for SSL certificate Validation.

I do checked couple of solutions online for this but all are implemented with

HttpComponentsClientHttpRequestFactory

which don't have provision to pass proxy host and server (Java.net.proxy object).

Can anyone help me here, I am stuck from long on this.

Solution I found online is below

TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

            SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                            .loadTrustMaterial(null, acceptingTrustStrategy)
                            .build();

            SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

            CloseableHttpClient httpClient = HttpClients.custom()
                            .setSSLSocketFactory(csf)
                            .build();

            HttpComponentsClientHttpRequestFactory requestFactory =
                            new HttpComponentsClientHttpRequestFactory();

            requestFactory.setHttpClient(httpClient);
            restTemplate = new RestTemplate(requestFactory);

Thanks, Anshu

Anshu
  • 69
  • 2
  • 18

0 Answers0