How to configure Spring RestTemplate Security

In the tutorial, JavaSampleApproach will guide you how to configure Spring RestTemplate Security.

Related post: How to use Spring RestTemplate client for consuming Restful WebService

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.3.RELEASE

II. Overview

We have 2 Spring Boot Projects:
– Restful Web Services with Security.
– Spring RestTemplate with Security Authentication.

1. Project Structure

spring boot resttemplate security overview

2. Step to do

– Create a Spring Boot project for Restful Web-Services.
– Create a Spring Boot project for Spring RestTemplate
– Create a simple Bussiness Model
– Implement RestController for Restful Web-Services
– Configure Restful-Web Services Security
– Config Spring RestTemplate Security
– Create a RestfulClient
– Make calls Restful Service by RestfulClient
– Build & Enjoy Results

III. Practices
1. Create a Spring Boot project for Restful Web-Services.

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project’s info. Press Next, then Finish, Spring Boot project will be created.

Open pom.xml, add Web & Security dependencies:

2. Create a Spring Boot project for Spring RestTemplate client

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project’s info. Press Next, then Finish, Spring Boot project will be created.

Open pom.xml, add Web & HttpClient dependencies:

3. Create a simple Bussiness Model

Create a business model: Customer

4. Implement RestController for Restful Web-Services

mplement 3 RequestMapping:
“/” – just for test
“/post” – for POST request method
“/get” – for GET request method

5. Configure Restful-Web Services Security

For detail, you can refer at post: Spring Security – Config Security for Web MVC by Spring Boot

In the tutorial, just create a simple configure as below:

6. Config Spring RestTemplate Security

– For customize Rest-Template Client, we use constructure:

– Configure HttpClient for Username/Password credential

– Config ClientHttpRequestFactory for Spring RestTemplate:

– Config Spring RestTemplate with ClientHttpRequestFactory:

7. Create a RestfulClient

a RestfulClient includes a RestTemplate, and have 2 functions:
POST request method
GET request method

8. Make calls Restful Service by RestfulClient

9. Run and Check results

– First build & run Restful Services project
– Second build & run RestTemplate Client project

Results:
– Resful Services

– Resttemplate Client
POST request:

GET request:

IV. Sourcecode

SpringBootSecurityRestfulService
SpringBootRestTemplateSecurity


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*