| π Articles | π€ My Profile |
πΉ Understand Spring Security basic concepts π Read here
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:
/login or /authenticate).UserDetailsService and JPA/Hibernate).π₯οΈ Backend
π API Documentation
π’οΈ Database
ποΈ Build & Dependency Management
βοΈ Utilities
@Getter, @Setter, @Builder, etc.)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
You can find the complete code repository for this project on GitHub:
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
SpringBootSecurityUsingDatabaseInRestApiApplication.java3οΈβ£ β‘ Run with Maven Command (CLI)
βΆοΈ Run app:
mvn spring-boot:run
π Run app with debug mode:
mvn spring-boot:run -Dspring-boot.run.fork=false -Dmaven.surefire.debug
π¦ Build JAR and run:
mvn clean package -DskipTests
java -jar target/spring-boot-security-using-database-in-rest-api-0.0.1-SNAPSHOT.jar
For a detailed running and demonstration of the application walkthrough,
watch the following YouTube video: