Created
February 20, 2020 02:31
-
-
Save jamesnguyen101/957f7bc9a9718f13466b87f7d3ce3dd8 to your computer and use it in GitHub Desktop.
Spring restTemplate Trust override
This file contains 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 | |
public RestTemplate restTemplate(RestTemplateBuilder builder) throws NoSuchAlgorithmException, KeyManagementException { | |
TrustManager[] trustAllCerts = new TrustManager[] { | |
new X509TrustManager() { | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return new X509Certificate[0]; | |
} | |
public void checkClientTrusted( | |
java.security.cert.X509Certificate[] certs, String authType) { | |
} | |
public void checkServerTrusted( | |
java.security.cert.X509Certificate[] certs, String authType) { | |
} | |
} | |
}; | |
SSLContext sslContext = SSLContext.getInstance("SSL"); | |
sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); | |
CloseableHttpClient httpClient = HttpClients.custom() | |
.setSSLContext(sslContext) | |
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) | |
.build(); | |
HttpComponentsClientHttpRequestFactory customRequestFactory = new HttpComponentsClientHttpRequestFactory(); | |
customRequestFactory.setHttpClient(httpClient); | |
return builder.requestFactory(() -> customRequestFactory).build(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment