Spring Rest + Spring Security example

In this post , we are going to apply Spring Security on Spring Rest example.

Web services tutorial:

Here are steps to create a simple Spring Restful web services with Spring Security which will return json.
1) Create a dynamic web project using maven in eclipse.

2) We need to add Spring Security and Jackson json utility in the classpath.
Spring will load Jackson2JsonMessageConverter into its application context automatically. Whenever you request resource as json with accept headers=”Accept=application/json”, then Jackson2JsonMessageConverter comes into picture and convert resource to json format.

Now change pom.xml as follows:
pom.xml

3) Change web.xml as below:

4) create a xml file named springrest-servlet.xml in /WEB-INF/ folder.
Please change context:component-scan if you want to use different package for spring to search for controller.Please refer to spring mvc hello world example for more understanding.

Configure Spring security:

Create a file named spring-security.xml in WEB-INF folder as below:

You can read more about Spring security to understand above configuration better. When user will try to access countries or country/{id} url, he will get login form and he need to put correct credentials (Username : java2blog and password= java123) to access Spring Rest APIs.

Create bean class

5) Create a bean name “Country.java” in org.arpit.java2blog.bean.

Create controller

6) Create a controller named “CountryController.java”

@PathVariable: Used to inject values from the URL into a method parameter.This way you inject id in getCountryById method .

We are not providing any view information in springrest-servlet.xml as we do in Spring MVC. If we need to directly get resource from controller, we need to return @ResponseBody as per Spring 3 but with Spring 4, we can use @RestController for that.
In spring 4.0, we can use @RestController which is combination of @Controller + @ResponseBody.

6) It ‘s time to do maven build.

Right click on project -> Run as -> Maven build
7) Provide goals as clean install (given below) and click on run

Run the application

8) Right click on project -> run as -> run on server
Select apache tomcat and click on finish

When you run the application, you might get this kind of warning

Please ignore above warning. When you start application, you have below URL if you have not provided start page:
http://localhost:8080/SpringRestSpringSecurityExample/ 

As we have used DispatcherServlet in web.xml, this request goes to spring DispatcherServlet and it did not find the corresponding mapping in controller , hence you get that warning.

9) Test your REST service under: “http://localhost:8080/SpringRestSpringSecurityExample/countries”.
When you try to access above URL, you will get login page as below

Spring Rest security login

If you put correct username and password, you will get below page:

Spring Rest security success login
We are done with Spring Restful web services with Spring Security example. If you are still facing any issue, please comment.

If you getting 404 error with above steps, you may need to follow below steps:


1) If you are getting this warning into your Tomcat startup console log, then it can cause the issue
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.j2ee.server:SpringRestfulWebServicesExample’ did not find a matching property.
This particular warning basically means that the element in Tomcat’s server.xml contains an unknown attribute source and that Tomcat doesn’t know what to do with this attribute and therefore will ignore it.
To resolve this in eclipse,
Remove the project from the server from the Server View. Right click on server -> add and remove
then remove project from server configuration.
Then run the project under the same server. Warning should be removed now
Or if warning still remains then
  • Go to server view
  • Double click on your tomcat server. It will open the server configuration.
  • Under server options check ‘Publish module contents to separate XML files’ checkbox.
  • Restart your server. This time your page will come without any issues.
2) Try to update Maven project.
Right click on project ->Maven-> update project
then

This should solve you issues.

Was this post helpful?

Leave a Reply

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