Javatpoint Logo
Javatpoint Logo

Spring Security Form-Based Authentication

Form-Based authentication is a way in which user's authentication is done by login form. This form is built-in and provided by spring security framework.

The HttpSecurity class provide a method formLogin() which is responsible to render login form and validate user credentials.

In this tutorial, we will create an example that implements form-based authentication. Lets start the example.

Create a Maven Project

First create a maven project by providing project details.


Spring Security Form-Based Authentication

This project initially looks like this:


Spring Security Form-Based Authentication 2

Spring Security Configuration

Configure spring security in the application by using the following Java files. Create a package com.javatpoint and put all the files into it.

// AppConfig.java

// MvcWebApplicationInitializer.java

// SecurityWebApplicationInitializer.java

// WebSecuiryConfig.java

Controller

Create a controller HomeController and put inside the com.javatpoint.controller package. It contains the following code.

// HomeController.java

Views

This project contains the following two view (JSP pages). Put these into WEB-INF/views folder.

// index.jsp

// admin.jsp

Project Dependencies

// pom.xml

Project Structure

After adding all these files the project structure looks like this:


Spring Security Form-Based Authentication 3

Run Server

Run the application over the server and see it produces the following output to the browser.

Output:


Spring Security Form-Based Authentication 4

Click on link, a login form is rendered that will use for form-based authentication.


Spring Security Form-Based Authentication 5

After validating credentials it authenticate the user and render to the admin page.


Spring Security Form-Based Authentication 6





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA