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:

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

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 spring security custom login form 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.


Create Controller  and view

step 3:
Change controller class named “HelloWorldController.java” as below

As  request first goes to dispatcherServlet and it redirects to controller class. Here @Controller depicts that this is our controller class. @RequestMapper is used to map incoming http request to handler method(hello() in above controller).So hello() method of HelloWorldController.java will handle GET request from dispatcher.
Here We have used Principal object to get current logged in username. It is set by Spring security framework.

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

Step 5:
Create login.jsp in /WEB-INF/pages folder

We have used css file in above login.jsp. Create style.css file as below.

Step 6:  

Now we need to add spring configuration xml. Create a file named spring-security.xml.

We have used form-login in above file, so if user tries to access any secured url, he will be authenticated based on above form-login configuration.
Lets understand meaning of each attribute of form-login tag

login-page : we need to provide url for login page.
default-target-url : here if authentication is successful, then target page url should be provided.
authentication-failure-url : if authentication is unsuccessful, then redirection page url should be provided here.

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 arpit) and password(java123 and arpit123)   in authentication manager, so if user provides correct credential for admin then only he will be able to access helloworld.html.

Step 7: 

Step 8: 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 9:
It’s time for maven build.
Provide goals as clean install (given below) and click on run

Run the application

Step 10:
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:


You may also like:

Related Posts

  • Python check for null
    17 November

    How to check for null in Python

    Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

  • Split String with multiple delimiters
    17 November

    Split String With Multiple Delimiters In Python

    Table of ContentsSplitting a string in PythonWays to split a string in PythonUsing the split() and replace() functionsUsing the regular expressions In this post, we will see how to split String with multiple delimiters in Python. Splitting a string in Python When we talk about splitting a string, it means creating a collection of sub-strings […]

  • Funny Kahoot names
    17 November

    600+ Funny, Dirty, Inappropriate Names for Kahoot

    Table of ContentsFunny Names for KahootKahoot: A Brief OverviewHow to Change your Name on Kahoot?Funny Names for Kahoot: Overall EditionFunny Kahoot Names: Girls EditionFunny Names For Kahoot: Boys EditionFunny Names for Kahoot: Unique EditionFunny Troll Names for KahootFunny Inappropriate Names: Dirty Kahoot namesGood Kahoot NamesFunny Inappropriate Names: Set 2Frequently Asked Questions About Funny Names for […]

  • Funny steam names
    17 November

    600+ Funny Steam Names to Use in 2021

    Table of ContentsFunny Steam Names: Let’s Enhance your Steam Profile by Giving it a New NameAssigning Funny Steam Names:Step-By-Step Guide200+ Funny Steam Names70+ Cool Steam Names90 Good Steam Names50 Clever Steam Names30 Funny Steam Names For Boys35 Funny Steam Names For Girls25 Creative Cool Steam Names100+ Other Funny Steam Names to Consider in 2021Deleting Previous […]

  • Sort dictionary by value in Python
    17 November

    Sort dictionary by value in python

    Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

  • Get class name in Python
    17 November

    Get class name in Python

    Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]


  1. Appreciating the dedication you put into your blog and in depth information you offer. It’s nice to come across a blog every once in a while that isn’t the same old rehashed information. Great read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

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.