Spring Security hello world example

In this post, we will see how to apply basic spring security to spring mvc hello world example.

Spring MVC tutorial:

If you want to secure your spring web application , you just need to configure some files to make it happen using spring security. We will apply login security on hello world example. So when only authorised users will be able to access hello world message.

Here are steps to apply basic spring security on spring mvc hello world example.

Step 1: 
Create Spring mvc hello world example named SpringSecurityHelloWorldExample . It will create basic spring mvc application.

Step 2: 
Add spring security to pom.xml. You need to add following dependencies to the pom.

Step 3: 
Modify hello.jsp in /WEB-INF/ folder
Step 4:  

Now we need to add spring configuration xml. Create a file named spring-security.xml.
intercept-url configure for which pattern what kind of security is configured. For example: If http request url has pattern /hello*(hello.jsp,helloworld.html), it will be accessed to ROLE_ADMIN only.

We have hardcoded username(java2blog) and password(java123)   in authentication manager, so if user provides correct credential for admin then only he will be able to access helloworld.htm/.

We have not configured any login page here, so spring security will generate one for us.

Step 5: We need to change in web.xml to configure spring security.

Here we have used DelegatingFilterProxy which intercepts http request and pass it to springSecurityFilterChain. springSecurityFilterChain is a bean created by spring with http element used in spring-security.xml. It maintains list of all filters and is responsible for chain of filters.
We are done with changes required for spring security.
Step 6:
It’s time for maven build.
Provide goals as clean install (given below) and click on run

Run the application

Step 7:
Right click on project -> run as -> run on server
Select apache tomcat and click on finish
   You will see below screen:

When you click on above link, you will get following login page.

If you put wrong password and try to login, you will get below screen

If you put correct user id and password

Bingo !! you have logged in successfully.

When you click on logout, you will be back to first screen.

Download source code:

click to begin
20KB .zip

Please comment if you have any issues with above post.


You may also like:


You may also like:

Related Posts

  • 10 May

    Spring Boot + Spring Security example

    In this post, we will see how to create Spring boot + Spring Security example. Let’s see how Spring boot makes over life simpler.I am going to apply Spring Security on Spring Boot hello world example. Github Source code: Download Spring Boot + Spring Security example: Here are steps to create a Spring boot + […]

  • 14 September

    Spring Rest + Spring Security example

    In this post , we are going to apply Spring Security on Spring Rest example. Web services tutorial: Introduction to web services Web services interview questions SOAP web service introduction RESTful web service introduction Difference between SOAP and REST web services SOAP web service example in java using eclipse JAX-WS web service eclipse tutorial JAX-WS […]

  • 13 September

    Spring Security : Customize 403 access denied page

    In this post , we will see how to customize 403 access denied page. If user do not have access to page, then it will show default 403 page which will look like as below: You can customize 403 as below page: If you want to configure custom 403 access denied page, there are two […]

  • 12 September

    spring security database authentication example

    In previous post, we have already seen  how to use custom login page in Spring security. We have declared username and password in spring-security.xml but what if you want to read it from database. In most of the cases, we will read credentials from database. In this post, we will do authentication using database. We […]

  • 15 January

    Spring Security custom login form example

    In previous post,we have used default login page generated by Spring security framework. In this post, we will see how to use custom login page in Spring security. Spring MVC tutorial: Spring MVC hello world example Spring MVC Hibernate MySQL example Spring MVC interceptor example Spring MVC angularjs example Spring MVC @RequestMapping example Spring Component,Service, […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.