How yelm-212/spring-jwt-login-logout works
Project Type
• It is a full-stack backend authentication system with a focus on JWT login/logout functionality.
• The system handles authentication via Spring Security with filters (LoginFilter and JWTFilter), uses a database (PostgreSQL) to store user details, and integrates Redis for managing token blacklisting.
File Structure
• Top-Level Components:
• The “src/main/java” directory contains the code for the Spring Boot application. Its package structure (com.yelm.jwtlogin) indicates modular organization.
• Subdirectories such as:
▪ config – contains security and Redis configurations along with custom authentication providers.
▪ controller – holds various controllers like AdminController, MainController, and user-specific controllers (JoinController, ReissueController, UserController).
▪ jwt – contains JWT-related logic (JWTFilter, JWTUtil, logout handlers, and login filters) which are core to the authentication flow.
▪ user – houses user entities, mappers, services, and value objects (VOs) for registration and updates.
• Dependencies and Configuration Files:
• build.gradle, docker-compose.yml, application.yml, and various gradle wrapper files denote build and runtime configuration.
• Docker Compose is set up to spin up Redis for token management, reinforcing the importance of in-memory token handling.
• The project also contains tests under “src/test”, ensuring unit tests for main application components.
Core Components
a. External Client:
• Interactions (sending login, logout, reissue and protected requests).
b. Authentication Entry Points:
• LoginFilter: Initiates login requests and handles authentication flow.
• JWTFilter: Intercepts protected requests to validate the Access Token.
c. Authentication Manager and Services:
• AuthenticationManager delegates authentication to CustomUserDetailsService.
• CustomUserDetailsService retrieves user data from PostgreSQL.
• TokenBlacklistService checks tokens against Redis (or another caching mechanism).
d. Data Stores:
• Redis: Used to store blacklisted tokens and possibly manage refresh tokens.
e. Additional Components:
• Controllers: AdminController, MainController, and user controllers (for join, reissue, and user-related endpoints). These facilitate various API endpoints.
• Configuration classes: SecurityConfig, RedisConfig and CustomAuthenticationProvider set up the overall application security and integration.
Relationships and Data Flow
• LoginFilter invoking the AuthenticationManager which then uses CustomUserDetailsService to load user details from PostgreSQL.
• For reissue and logout flows, display how Refresh Tokens and blacklisting interact with the TokenBlacklistService and Redis.
Architectural Patterns and Design Principles
• Use a layered architecture separating the controller, service, configuration, and data access layers.
• Use of filters for authentication and the use of dependency injection (Spring Boot conventions).
• Integration of external components and services, like PostgreSQL and Redis, in handling persistent and in-memory data respectively.
• Mention the micro-level modularity as evident from the division between JWT handling, user management, and security configurations.