Created
January 13, 2017 07:51
-
-
Save kasimok/04878482b9e7bc49a83ad5acfc74234e to your computer and use it in GitHub Desktop.
Spring resttemplate with timeout and accept all certificate(ignore ssl error)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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