Skip to content

Instantly share code, notes, and snippets.

@K0NRAD
Created April 8, 2019 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K0NRAD/6e762fc1db73b9cafadd6372292f0e43 to your computer and use it in GitHub Desktop.
Save K0NRAD/6e762fc1db73b9cafadd6372292f0e43 to your computer and use it in GitHub Desktop.
spring boot - create insecure client with resttemplate
@Bean("non-ssl-validation")
public RestTemplate insecureRestTemplate() {
try {
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);
return new RestTemplate(requestFactory);
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException ex) {
throw new RuntimeException(ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment