Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling SSL Certificate Validation in Spring RestTemplate

I am having two Spring-based web apps A and B, on two different machines.

I want to make an HTTPS call from web app A to web app B, however, I am using a self-signed certificate in Machine B. So my HTTPS request fails.

How can I disable HTTPS certificate validation when using RestTemplate in Spring? I want to disable validation because both web app A and B are within the internal network, but data transfer has to happen over HTTPS

like image 408
Prabhu R Avatar asked Nov 01 '10 19:11

Prabhu R


People also ask

How do I disable SSL validation in curl?

To bypass SSL certificate validation for local and test servers, you can pass the -k or --insecure option to the Curl command. This option explicitly tells Curl to perform "insecure" SSL connections and file transfers. Curl will ignore any security warnings about an invalid SSL certificate and accept it as valid.

How do I disable SSL certificate with spring RestTemplate?

To skip or avoid the SSL check, we need to modify the default RestTemplate available with the normal Spring package. In this configuration class, we basically declare a new Bean that creates a HTTPClient with the certificate check as disabled.


1 Answers

@Bean public RestTemplate restTemplate()                  throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {     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 restTemplate = new RestTemplate(requestFactory);     return restTemplate;  } 
like image 152
DanieleDM Avatar answered Sep 21 '22 02:09

DanieleDM