Skip to content

Instantly share code, notes, and snippets.

@kasimok
Created January 13, 2017 07:51
Show Gist options
  • Save kasimok/04878482b9e7bc49a83ad5acfc74234e to your computer and use it in GitHub Desktop.
Save kasimok/04878482b9e7bc49a83ad5acfc74234e to your computer and use it in GitHub Desktop.
Spring resttemplate with timeout and accept all certificate(ignore ssl error)
@Autowired
RestTemplate restTemplate;
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
try {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
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);
requestFactory.setReadTimeout(3000);
requestFactory.setConnectTimeout(3000);
return requestFactory;
} catch (Exception e) {
e.printStackTrace();
LOG.error("Error SSL Config");
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment