Skip to content

Instantly share code, notes, and snippets.

@jamesnguyen101
Created February 20, 2020 02:31
Show Gist options
  • Save jamesnguyen101/957f7bc9a9718f13466b87f7d3ce3dd8 to your computer and use it in GitHub Desktop.
Save jamesnguyen101/957f7bc9a9718f13466b87f7d3ce3dd8 to your computer and use it in GitHub Desktop.
Spring restTemplate Trust override
@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