nirmalakumarsahu

Spring Boot Security Securing REST API with Database Authentication

πŸ“„ Articles πŸ‘€ My Profile

Spring Boot Security

Spring Framework Framework


πŸ“‘ Index


πŸ“‹ Prerequisites

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

πŸ” Back to Top


πŸ” What is Spring Boot Security using Database in REST API?

It means you are securing your Spring Boot REST API by storing and validating user credentials (username, password, roles, permissions, etc.) in a database (like MySQL, PostgreSQL, etc.) instead of keeping them hardcoded or in memory.

So when a user tries to log in, Spring Security will:

  1. Take the username & password from the login request (e.g., /login or /authenticate).
  2. Look up the user details from the database (via UserDetailsService and JPA/Hibernate).
  3. Verify the password (usually encrypted with BCrypt).
  4. Assign the correct roles/authorities from the DB.
  5. Generate a session (stateful) or token (stateless, e.g., JWT).
  6. Authorize or reject the API request.

πŸš€ Implementation

πŸ—οΈ Technology Stack

πŸ–₯️ Backend

πŸ“– API Documentation

πŸ›’οΈ Database

πŸ—οΈ Build & Dependency Management

βš™οΈ Utilities

πŸ“‚ Project Structure

spring-boot-security-using-database-in-rest-api
│── πŸ“‚ src/
β”‚   └── πŸ“‚ main/
β”‚       β”œβ”€β”€ πŸ“‚ java/
β”‚       β”‚   └── πŸ“‚ com/sahu/springboot/security/
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ config/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ CustomAuthenticationEntryPoint.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ OpenApiConfig.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ OpenApiProperties.java
β”‚       β”‚       β”‚   └── πŸ“„ SecurityConfig.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ constant/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ ApiStatus.java
β”‚       β”‚       β”‚   └── πŸ“„ AuthConstants.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ controller/
β”‚       β”‚       β”‚   └── πŸ“‚ rest/
β”‚       β”‚       β”‚       β”œβ”€β”€ πŸ“„ AuthRestController.java
β”‚       β”‚       β”‚       └── πŸ“„ DashboardRestController.java
β”‚       β”‚       β”‚
β”‚       β”‚       β”œβ”€β”€ πŸ“‚ dto/
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ ApiResponse.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ LoginRequest.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ LoginResponse.java
β”‚       β”‚       β”‚   β”œβ”€β”€ πŸ“„ UserRequest.java
β”‚       β”‚       β”‚   └── πŸ“„ UserResponse.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
β”‚       β”‚       β”‚
β”‚       β”‚       └── πŸ“„ SpringBootSecurityUsingDatabaseInRestApiApplication.java
β”‚       β”‚
β”‚       └── πŸ“‚ resources/
β”‚           └── πŸ“„ 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-securing-rest-api-with-database-authentication

πŸš€ 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: SpringBootSecurityUsingDatabaseInRestApiApplication.java
  3. Select Run β€˜SpringBootSecurityUsingDatabaseInRestApiApplication’.
  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 ➑️