Javatpoint Logo
Javatpoint Logo

Spring Security Custom Login

Spring Security provides it's own built-in login module to authenticate the user. It validates the user credentials and provide accessibility into the application.

The login page rendered by the module is built-in. So, we does not require to create new jsp page. But if we want to customize the login page then how we can?

The answer is, we can create our own jsp login page and integrate to the application. In this topic we will create a custom login page and will use it to get login.

See, an example. Create a maven project by providing following details.


Spring Security Custom Login

After finishing, it creates the following project structure.


Spring Security Custom Login 2

Security Configuration

Configure project to apply spring security. It require following four files. Create a package com.javatpoint and put these files into this.

// AppConfig.java


// MvcWebApplicationInitializer.java

// SecurityWebApplicationInitializer.java

// WebSecurityConfig.java


See, in configure method, after formLogin() a method loginPage("/login") is used. It is the actual method that required to call custom login page.

View

First create a login page our own. According to the spring official, the login page should looks like the below.

// login.jsp

// index.jsp

// admin.jsp

Controller

Create a controller HomeController inside the com.javatpoint.controller package.

// HomeController.java

Project Dependencies

// pom.xml

Project Structure

Our project looks like the this:


Spring Security Custom Login 3

Run the Server

Output:


Spring Security Custom Login 4

Now, login by providing user credentials.


Spring Security Custom Login 5
Spring Security Custom Login 6

See, it's working fine. Now, we can create it more decorative and custom according to the need.

Download this example.






Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA