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.
This project initially looks like this:
Spring Security Configuration
Configure spring security in the application by using the following Java files. Create a package com.tutoraspire 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.tutoraspire.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:
Run Server
Run the application over the server and see it produces the following output to the browser.
Output:
Click on link, a login form is rendered that will use for form-based authentication.
After validating credentials it authenticate the user and render to the admin page.