nirmalakumarsahu

Spring Boot Security Form-Based Authentication with Database

πŸ“„ Articles πŸ‘€ My Profile

Spring Boot Security

Spring Framework Framework


πŸ“‘ Index


πŸ“‹ Prerequisites

πŸ”Ή Understand Spring Security basic concepts πŸ‘‰ Read here

πŸ” Back to Top


πŸ“ Spring Boot Security Internal Flow

πŸ’‘ Let’s understand how Spring Boot Security works with a database for form-based authentication.

spring-boot-security-internal-flow.png

1. Request with Credentials

2. DelegatingFilterProxy & FilterChainProxy

3. Filter Converts Credentials

4. AuthenticationManager

5. ProviderManager

6. Fetch User Details

8. Database Call

9. UserDetails Object

10. Return Authenticated Object

11. Authentication Response

12. Filter Receives Authentication Response

13. Set Authenticated User into Context

14. Continue Filter Chain

15. Success Response

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Object principal = auth.getPrincipal();

πŸ” Back to Top


πŸš€ Implementation

πŸ—οΈ Technology Stack

πŸ–₯️ Backend

🎨 Frontend

πŸ›’οΈ Database

πŸ—οΈ Build & Dependency Management

βš™οΈ Utilities

πŸ“‚ Project Structure

spring-boot-security-using-database-form-based
│── πŸ“‚ src/
β”‚   └── πŸ“‚ main/
β”‚       β”œβ”€β”€ πŸ“‚ java/
β”‚       β”‚   └── πŸ“‚ com/sahu/springboot/security/
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ config/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ CustomAuthenticationEntryPoint.java
β”‚       β”‚       β”‚   └── πŸ“„ SecurityConfig.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ constant/
β”‚       β”‚       β”‚   └── πŸ“„ AuthConstants.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ controller/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ AuthController.java
β”‚       β”‚       β”‚   └── πŸ“„ HomeController.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ dto/
β”‚       β”‚       β”‚   └── πŸ“„ UserRequest.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ model/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ Role.java
β”‚       β”‚       β”‚   └── πŸ“„ User.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ repository/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ RoleRepository.java
β”‚       β”‚       β”‚   └── πŸ“„ UserRepository.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ security/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“‚ dto/
β”‚       β”‚       β”‚   β”‚   └── πŸ“„ CustomUserDetails.java
β”‚       β”‚       β”‚   β”‚
β”‚       β”‚       β”‚   └── πŸ“‚ util/
β”‚       β”‚       β”‚       └── πŸ“„ SecurityUtil.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ service/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“‚ impl/
β”‚       β”‚       β”‚   β”‚   β”œβ”€β”€ πŸ“„ CustomUserDetailsService.java
β”‚       β”‚       β”‚   β”‚   └── πŸ“„ UserServiceImpl.java
β”‚       β”‚       β”‚   β”‚
β”‚       β”‚       β”‚   └── πŸ“„ UserService.java
β”‚       β”‚       β”‚
β”‚       β”‚       └── πŸ“„ SpringBootSecurityUsingDatabaseFormBasedApplication.java
β”‚       β”‚
β”‚       └── πŸ“‚ resources/
β”‚           β”œβ”€β”€ πŸ“‚ static/
β”‚           β”‚
β”‚           β”œβ”€β”€ πŸ“‚ templates/
β”‚           β”‚   β”œβ”€β”€ πŸ“‚ error/
β”‚           β”‚   β”‚   └── πŸ“„ 403.html
β”‚           β”‚   β”‚
β”‚           β”‚   β”œβ”€β”€ πŸ“„ admin-dashboard.html
β”‚           β”‚   β”œβ”€β”€ πŸ“„ dashboard.html
β”‚           β”‚   β”œβ”€β”€ πŸ“„ login.html
β”‚           β”‚   β”œβ”€β”€ πŸ“„ registration.html
β”‚           β”‚   └── πŸ“„ user-dashboard.html
β”‚           β”‚
β”‚           └── πŸ“„ application.yml
β”‚
β”œβ”€β”€ πŸ“„ docker-compose.yml
└── πŸ“„ pom.xml

πŸ”— Code Repository

You can find the complete code repository for this project on GitHub:

GitHub - spring-boot-security-form-based-authentication-with-database

πŸš€ To Run the Spring Boot Application

1️⃣ 🐳 Using Docker Compose (for MySQL container)

docker-compose up -d

βœ… This starts MySQL in a container (-d = detached mode). πŸ” Verify with:

docker ps

πŸ“Œ DB is now available at localhost:3307. πŸ”‘ Credentials (username, password, DB name) are in docker-compose.yml.

2️⃣ πŸ’» Run Directly in IntelliJ IDEA

  1. πŸ“‚ Open the Spring Boot project in IntelliJ.
  2. ▢️ In Project Explorer, right-click the main class: SpringBootSecurityUsingDatabaseFormBasedApplication.java
  3. Select Run β€˜SpringBootSecurityUsingDatabaseFormBasedApplication’.
  4. 🐞 For debugging, click the Debug button instead of Run.
  5. 🌐 App will start on http://localhost:9897.

3️⃣ ⚑ Run with Maven Command (CLI)

πŸ” Back to Top


πŸŽ₯ Video Reference

For a detailed running and demonstration of the application walkthrough,
watch the following YouTube video:

Watch the video

πŸ” Back to Top

πŸ“– Read More ➑️