Created
April 8, 2019 15:21
-
-
Save K0NRAD/6e762fc1db73b9cafadd6372292f0e43 to your computer and use it in GitHub Desktop.
spring boot - create insecure client with resttemplate
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
@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