Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Https Rest Service using Spring RestTemplate (2 way SSL between client and server)

Tags:

spring-boot

Can anybody provide me with a code sample to access rest service url secured with https using spring rest template.

I have the certificate(.pfx format) password and send cient side certificate to server. server side is used on the client side certificate and established the connection I want to create a springboot application that work as 2 way SSL between client and server. Thanks.

like image 444
user3825596 Avatar asked Apr 29 '17 07:04

user3825596


People also ask

How does 2 way SSL work?

In two-way SSL authentication, the client application verifies the identity of the server application, and then the server application verifies the identity of the client application. Both parties share their public certificates, and then validation is performed.

Does RestTemplate support all HTTP methods?

Spring RestTemplate class is part of spring-web , introduced in Spring 3. We can use RestTemplate to test HTTP based restful web services, it doesn't support HTTPS protocol. RestTemplate class provides overloaded methods for different HTTP methods, such as GET, POST, PUT, DELETE etc.


1 Answers

I created a sample Spring Boot application that demonstrates how to create a RestTemplate that is configured for SSL client authentication. The sample application acts as the server as well which requires SSL mutual authentication (to demonstrate usage via the test case). In practice, the RestTemplate bean would interact with an external service. Hope this helps.

https://github.com/steve-oakey/spring-boot-sample-clientauth

I should note that the most important part of the example is creating the SSLContext. There are plenty of ways to create the SSLContext, I chose a method that uses the SSLContextBuilder from the org.apache.httpcomponents:httpclient library. Other methods such as using the Java API directly, or setting the javax.net.ssl.* JVM properties would also work.

like image 112
SteveO Avatar answered Sep 20 '22 19:09

SteveO