Exemplo para configurar um RestTemplate lendo um certificado digital. Neste exemplo, o certificado é armazenado em um arquivo PKCS12:
@Bean
public RestTemplate restTemplate() {
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(new FileInputStream("/path/to/certfile"), "certpassword".toCharArray());
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.useProtocol("TLS");
sslContextBuilder.loadKeyMaterial(clientStore, "certpassword".toCharArray());
sslContextBuilder.loadTrustMaterial(new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(10000); // 10 seconds
requestFactory.setReadTimeout(10000); // 10 seconds
return new RestTemplate(requestFactory);
}