# pkini2002/jwt-authentication-springboot architecture

> Architecture diagram of the GitHub repository pkini2002/jwt-authentication-springboot, 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/pkini2002/jwt-authentication-springboot
- Repository: https://github.com/pkini2002/jwt-authentication-springboot

## Overview

##### Project Type and Purpose
- It is a backend web application built on Spring Boot (version 3.1.4) using Maven.
- It demonstrates user authentication using JWTs, where tokens are generated upon login and then validated for subsequent API requests.

##### Key Components (as seen from the file tree and README)
- Application Entry Point:
– JwtAuthenticationSpringbootApplication.java: The main class that starts the Spring Boot application.
- Configuration Components:
– AppConfig.java (if used for additional bean configurations)
– SecurityConfig.java: Configures Spring Security settings, including the security filter chain, session management (stateless), exception handling, and filter ordering.
- Controllers:
– HomeController.java: Likely exposes a secured endpoint used for testing or accessing restricted resources.
– AuthController.java: Provides an endpoint (/auth/login) for authenticating the user and returning the JWT token.
- Models:
– JwtRequest.java: Represents the login input (email and password) received from the client.
– JwtResponse.java: Represents the output of the login process (JWT token and username) sent back to the client.
– User.java: Represents user details.
- Security Components:
– JWTAthenticationEntryPoint.java: Handles unauthorized requests and invalid authentication scenarios.
– JWTAuthenticationFilter.java: Intercepts incoming HTTP requests to extract, validate, and process the JWT token.
– JWTHelper.java: Contains utility methods to generate, validate, and parse JWT tokens.
- Services:
– UserService.java: (Although not detailed, this suggests additional user-related business logic, probably used internally to fetch user details.)
- Additional Resources:
– application.properties: Contains configuration settings tied to Spring Boot, likely including security settings.

##### Relationships and Interactions
- The authentication flow begins at the AuthController when a client submits a login request. This passes through the Spring Security authentication manager (configured via SecurityConfig and AppConfig).
- Upon successful authentication, the JWT is generated by methods within JWTHelper and returned via JwtResponse.
- For subsequent requests to secured endpoints (e.g., in HomeController), the JWTAuthenticationFilter intercepts the HTTP request, extracts the token from the Authorization header, and uses JWTHelper to validate it.
- If the token is valid, Spring Security’s context is populated with an authenticated UserDetails. If not, the JwtAuthenticationEntryPoint handles the error.

##### Architectural Patterns and Frameworks
- The project adheres to the layered architecture typical of Spring Boot applications (controller, service, security, model layers).
- It uses Spring Security filters (specifically a custom OncePerRequestFilter) to intercept and process authentication tokens.
- The JWT-based authentication pattern is central, using libraries from io.jsonwebtoken to manage tokens.
- Dependency injection and configuration-management practices are evident throughout the code (using @Autowired and @Configuration).

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client Application"]:::client
    External_SB["Spring Boot Framework"]:::external
    External_SS["Spring Security"]:::external

    Main["JwtAuthenticationSpringbootApplication"]:::mainApp

    subgraph "Configuration Layer"
        AppConfig["AppConfig"]:::config
        SecurityConfig["SecurityConfig"]:::security
        AppProps["application.properties"]:::config
    end

    subgraph "Controller Layer"
        AuthController["AuthController"]:::controller
        HomeController["HomeController"]:::controller
    end

    subgraph "Security Components"
        JWTFilter["JWTAuthenticationFilter"]:::security
        JWTHelper["JWTHelper"]:::security
        JWTEntry["JWTAthenticationEntryPoint"]:::security
    end

    subgraph "Model Layer"
        JwtRequest["JwtRequest"]:::model
        JwtResponse["JwtResponse"]:::model
        User["User"]:::model
    end

    subgraph "Service Layer"
        UserService["UserService"]:::service
    end

    %% Authentication Flow
    Client -->|"POST/auth/login"| AuthController
    AuthController -->|"calls_JWTHelper"| JWTHelper
    JWTHelper -->|"returns_JWT_Token"| AuthController
    AuthController -->|"returns_JwtResponse"| Client

    %% Secured Request Flow
    Client -->|"GET/home/test_with_JWT"| JWTFilter
    JWTFilter -->|"validates_token_via"| JWTHelper
    JWTHelper -->|"validation_result"| JWTFilter
    JWTFilter -- "if_valid" --> HomeController
    HomeController -->|"fetches_user_data"| UserService
    HomeController -->|"returns_secured_content"| Client
    JWTFilter -- "if_invalid" --> JWTEntry
    JWTEntry -->|"error_response"| Client

    %% Configuration and Initialization
    Main --> AppConfig
    AppConfig --> SecurityConfig
    SecurityConfig --> JWTFilter
    External_SB --> Main
    External_SS --> SecurityConfig

    %% Click Events
    click Main "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/JwtAuthenticationSpringbootApplication.java"
    click AppConfig "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/config/AppConfig.java"
    click SecurityConfig "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/config/SecurityConfig.java"
    click AuthController "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/config/AuthController.java"
    click HomeController "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/controllers/HomeController.java"
    click JwtRequest "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/models/JwtRequest.java"
    click JwtResponse "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/models/JwtResponse.java"
    click User "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/models/User.java"
    click JWTEntry "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/security/JWTAthenticationEntryPoint.java"
    click JWTFilter "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/security/JWTAuthenticationFilter.java"
    click JWTHelper "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/security/JWTHelper.java"
    click UserService "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/java/com/example/JWTAuthenticationSpringboot/services/UserService.java"
    click AppProps "https://github.com/pkini2002/jwt-authentication-springboot/blob/main/src/main/resources/application.properties"

    %% Styles
    classDef client fill:#f1c40f,stroke:#000,stroke-width:2px;
    classDef controller fill:#85C1E9,stroke:#000,stroke-width:2px;
    classDef security fill:#E74C3C,stroke:#000,stroke-width:2px;
    classDef model fill:#82E0AA,stroke:#000,stroke-width:2px;
    classDef config fill:#F7DC6F,stroke:#000,stroke-width:2px;
    classDef service fill:#BB8FCE,stroke:#000,stroke-width:2px;
    classDef mainApp fill:#D35400,stroke:#000,stroke-width:2px;
    classDef external fill:#95A5A6,stroke:#000,stroke-width:2px;
```
