Time needed to complete ~25 minutes
1.Introduction
Rest Services are common today and the change to be asked to develop one and secure it is
high, this article will demonstrate how to do that.
high, this article will demonstrate how to do that.
2.What is used in this tutorial:
1- Maven 3
2- Jdk 1.8
3- Tomcat 7.0.55
4- Eclipse Mars 4.5.1
5- Spring 4.1.8
6- Spring Security 4.0.2
7-MySql 5
6- Spring Security 4.0.2
7-MySql 5
3.Project Structure
4. Pojo and Rest Service
- Person pojo class will be used as simple model with, Listing 4.1.
- Rest Service with 3 methods that will be secured in Listing 4.2.
- Rest Service with 3 methods that will be secured in Listing 4.2.
5. Spring Configuration
- Spring security configuration Listing 5.1. The path that will be /p/ , only authorized persons
with role ROLE_REST will be able to access the service, the users,roles and passwords are
stored in DB.
Note: don't forget to change username and password for database in security-context file
with role ROLE_REST will be able to access the service, the users,roles and passwords are
stored in DB.
Note: don't forget to change username and password for database in security-context file
6. Database tables
7. Deploying to tomcat
Note: don't forget to change username and password for tomcat server in pom.xml file
- Start tomcat.
- Start tomcat.
- Deploy application from run configuration in goal type clean install tomcat7:redeploy
click Apply and Run buttons.
7.1- In browser type http://localhost:8080/SecureRestExample/ will go to home page that is not secure , typingclick Apply and Run buttons.
http://localhost:8080/SecureRestExample/ p/test
http://localhost:8080/SecureRestExample/ p/all
http://localhost:8080/SecureRestExample/ p/1 will ask for username and password
entering
username: example@gmail.com
password: 1b4f0e9851971998e732078544c96b36c3d01cedf7caa332359d6f1d83567014
will access rest service, Picture 7.1.
7.2- OR can run CallRestServiceAppProgramatically class that uses RestTemplate from eclipse project ExampleRestClient.