The user1707141 example did not work for me, and skmansfield seems more likely to depend on specific files that are not consistent with Spring Boot / Maven. Also, Andy Wilkinson's answer uses the SSLConnectionSocketFactory constructor, which is deprecated in Apache httpclient 4.4+, and also seems rather complicated.
So, I created an example project that should show everything 100% understandable here: https://github.com/jonashackt/spring-boot-rest-clientcertificate
Besides the usual use of RestTemplate with @Autowired in your test class, be sure to configure RestTemplate as follows:
package de.jonashackt.restexamples; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.util.ResourceUtils; import org.springframework.web.client.RestTemplate; import javax.net.ssl.SSLContext; @Configuration public class RestClientCertTestConfiguration { private String allPassword = "allpassword"; @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception { SSLContext sslContext = SSLContextBuilder .create() .loadKeyMaterial(ResourceUtils.getFile("classpath:keystore.jks"), allPassword.toCharArray(), allPassword.toCharArray()) .loadTrustMaterial(ResourceUtils.getFile("classpath:truststore.jks"), allPassword.toCharArray()) .build(); HttpClient client = HttpClients.custom() .setSSLContext(sslContext) .build(); return builder .requestFactory(new HttpComponentsClientHttpRequestFactory(client)) .build(); } }
source share