Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot SSL Client

I am new to Spring Boot. So far I am enjoying it. I have developed a demo SSL rest web server that correctly handles mutual X.509 certificate authentication. Using an IE browser with self signed client & server certificates, I have tested that the demo rest web server is working correctly -- both the server and browser are successfully exchanging and validating each others certificates.

I am having trouble finding an SSL client example that shows how to include the client certificate and issue the https. Anybody have a simple rest client example that shows how to consume my ssl server?

Best Regards, Steve Mansfield

like image 206
skmansfield Avatar asked Jun 11 '15 01:06

skmansfield


People also ask

How do I enable http and https in spring boot?

To enable support for HTTP and HTTPS in Spring Boot 2, we need to register an additional connector with Spring Boot application. First, enable SSL/HTTPS for Spring Boot, for example by following the HTTPS using Self-Signed Certificate in Spring Boot tutorial. Now, add server. http.

What is SSLConnectionSocketFactory?

SSLConnectionSocketFactory is a layered socket factory for TSL and SSL connections. Using this, you can verify the Https server using a list of trusted certificates and authenticate the given Https server. You can create this in many ways.


1 Answers

Given that you're using Spring, here's an example that shows how to use Spring's RestTemplate and Apache's HttpClient configured with a client certificate and to trust a self-signed certificate from the server:

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new File("keystore.jks")),
        "secret".toCharArray());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
        new SSLContextBuilder()
                .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                .loadKeyMaterial(keyStore, "password".toCharArray()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
        httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
ResponseEntity<String> response = restTemplate.getForEntity(
        "https://localhost:8443", String.class);
like image 91
Andy Wilkinson Avatar answered Oct 20 '22 00:10

Andy Wilkinson