# yelm-212/spring-jwt-login-logout architecture

> Architecture diagram of the GitHub repository yelm-212/spring-jwt-login-logout, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yelm-212/spring-jwt-login-logout
- Repository: https://github.com/yelm-212/spring-jwt-login-logout

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        C["Client (Vue.js)"]:::client
    end

    %% Filters & Controllers Layer
    subgraph "Filters & Controllers"
        LF["LoginFilter"]:::filter
        JF["JWTFilter"]:::filter
        JL["JwtLogoutHandler"]:::filter
        JS["JwtLogoutSuccessHandler"]:::filter
        AC["AdminController"]:::controller
        MC["MainController"]:::controller
        JC["JoinController"]:::controller
        RC["ReissueController"]:::controller
        UC["UserController"]:::controller
    end

    %% Services Layer
    subgraph "Services Layer"
        AM["AuthenticationManager"]:::service
        CUDS["CustomUserDetailsSerivce"]:::service
        TBS["TokenBlacklistService"]:::service
        JU["JWTUtil"]:::service
    end

    %% Configurations
    subgraph "Configurations"
        SC["SecurityConfig"]:::config
        RCfg["RedisConfig"]:::config
        CAP["CustomAuthenticationProvider"]:::config
    end

    %% Data Stores Layer
    subgraph "Data Stores"
        subgraph "Relational DB (PostgreSQL)"
            PG["PostgreSQL"]:::database
            UE["UserEntity"]:::entity
            UM["UserMapper"]:::entity
        end
        subgraph "In-Memory Cache (Redis)"
            RDS["Redis"]:::database
        end
    end

    %% Flow Connections from Client
    C -->|"login/request"| LF
    C -->|"protected/request"| JF
    C -->|"logout/request"| JL
    C -->|"logout/request"| JS
    C -->|"API_call"| MC
    C -->|"API_call"| AC
    C -->|"API_call"| JC
    C -->|"API_call"| RC
    C -->|"API_call"| UC

    %% Authentication Flow
    LF -->|"authenticate"| AM
    AM -->|"loadUser"| CUDS
    CUDS -->|"query_DB"| PG
    LF -->|"generate_token"| JU
    JF -->|"validate_token"| JU

    %% Token Validation and Blacklisting
    JF -->|"check_blacklist"| TBS
    TBS -->|"query_cache"| RDS

    %% Logout Flow
    JL -->|"process_logout"| TBS
    JS -->|"process_logout"| TBS

    %% Controllers calling Services
    AC -->|"call_service"| CUDS
    MC -->|"call_service"| CUDS
    JC -->|"call_service"| CUDS
    RC -->|"reissue_token"| CUDS
    UC -->|"call_service"| CUDS

    %% Configuration Relationships
    SC -->|"configures"| LF
    SC -->|"configures"| JF
    SC -->|"sets_up"| AM
    SC -->|"integrates"| CAP
    RCfg -->|"configures"| RDS
    RCfg -->|"configures"| TBS

    %% Class Definitions for Colors
    classDef client fill:#a4d3ee,stroke:#1d76bb,stroke-width:2px;
    classDef filter fill:#ffcccc,stroke:#ff0000,stroke-width:2px;
    classDef controller fill:#ccffcc,stroke:#008000,stroke-width:2px;
    classDef service fill:#ffffcc,stroke:#cccc00,stroke-width:2px;
    classDef config fill:#f0e68c,stroke:#b8860b,stroke-width:2px;
    classDef database fill:#ffe4b5,stroke:#ff8c00,stroke-width:2px;
    classDef entity fill:#d3d3d3,stroke:#696969,stroke-width:2px;

    %% Click Events
    click LF "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/jwt/LoginFilter.java"
    click JF "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/jwt/JWTFilter.java"
    click JU "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/jwt/JWTUtil.java"
    click JL "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/jwt/JwtLogoutHandler.java"
    click JS "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/jwt/JwtLogoutSuccessHandler.java"
    click CAP "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/config/CustomAuthenticationProvider.java"
    click SC "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/config/SecurityConfig.java"
    click RCfg "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/config/RedisConfig.java"
    click TBS "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/blacklist/TokenBlacklistService.java"
    click CUDS "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/user/service/CustomUserDetailsSerivce.java"
    click AC "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/controller/AdminController.java"
    click MC "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/controller/MainController.java"
    click JC "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/user/controller/JoinController.java"
    click RC "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/user/controller/ReissueController.java"
    click UC "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/user/controller/UserController.java"
    click UE "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/user/entity/UserEntity.java"
    click UM "https://github.com/yelm-212/spring-jwt-login-logout/blob/master/src/main/java/com/yelm/jwtlogin/user/mapper/UserMapper.java"
```
