# narayangeed/spring-boot-jwt-demo architecture

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

- Interactive diagram: https://gitdiagram.com/narayangeed/spring-boot-jwt-demo
- Repository: https://github.com/narayangeed/spring-boot-jwt-demo

## Overview

##### Main Components
- Client/Application: This represents a client (for example, a browser or REST client) that makes requests to the API endpoints.
- REST Endpoints:
- /authenticate: A POST endpoint handled by the JwtAuthenticationController to collect the username and password. On successful authentication (via the Spring Authentication Manager and JwtUserDetailsService), a JWT token is generated using JwtTokenUtil.
- /hello: A GET endpoint served by HelloWorldController. Access to this endpoint is secured—only allowing requests with a valid JWT.
- Security Components:
- WebSecurityConfig: Configures Spring Security. It sets up the Authentication Manager, specifies which endpoints are secured, and wires in the JWT filter and entry point.
- JwtRequestFilter: A Spring Web Filter (extending OncePerRequestFilter) that intercepts incoming requests to check for a valid JWT token. If the token is valid, it sets the appropriate authentication in the security context.
- JwtAuthenticationEntryPoint: Handles unauthorized requests by returning a 401 error.
- JWT Utility:
- JwtTokenUtil: Responsible for the creation and validation of JWT tokens. It works with the io.jsonwebtoken.Jwts library.
- User Details Service:
- JwtUserDetailsService: Implements Spring Security’s UserDetailsService. It provides user details (in this demo hardcoded) required for authentication.
- Data Models:
- JwtRequest: Represents the username and password received in the authentication request.
- JwtResponse: Wraps the JWT token that is returned as a response.
- Build & Configuration:
- Maven configuration files (pom.xml and the Maven wrapper) indicate this is a Maven project with a typical Java structure.
- Application properties file includes configurations like jwt.secret.

##### Relationships and Interactions
- The client initiates a POST request to /authenticate with login credentials.
- The JwtAuthenticationController delegates authentication to Spring’s Authentication Manager, which invokes the JwtUserDetailsService.
- On success, JwtTokenUtil is called to generate a JWT token, and this token is returned by the JwtResponse.
- For subsequent requests (to /hello), the JwtRequestFilter intercepts the request, validates the token using JwtTokenUtil, and permits access if the token is valid.
- If a token is missing or invalid, JwtAuthenticationEntryPoint handles the error by rejecting the request (HTTP 401).
- The WebSecurityConfig class integrates all security components and configures which endpoints are open vs. secured.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Clear modularization where controllers handle incoming HTTP requests, security is managed separately using filters, and token logic is encapsulated in a utility class.
- Stateless Authentication: JWT based authentication avoids server-side session maintenance.
- Use of Dependency Injection: Spring Boot features and Maven for managing dependencies and configuration.
- RESTful API Design: Simple and clear API endpoints for core functionalities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    client["Client (Browser/REST Client)"]:::client

    %% Authentication Flow
    subgraph "Authentication Flow"
        jwtReq["JwtRequest (Credentials)"]:::model
        authCtrl["JwtAuthenticationController (/authenticate)"]:::controller
        userSvc["JwtUserDetailsService"]:::service
        jwtUtil["JwtTokenUtil"]:::security
        jwtResp["JwtResponse (Token)"]:::model

        client -->|"POST_/authenticate"| authCtrl
        jwtReq -->|"Input"| authCtrl
        authCtrl -->|"validate"| userSvc
        authCtrl -->|"generateJWT"| jwtUtil
        authCtrl -->|"returnToken"| jwtResp
        jwtResp -->|"Response"| client
    end

    %% Secure Request Flow
    subgraph "Secure Request Flow"
        filter["JwtRequestFilter"]:::security
        helloCtrl["HelloWorldController (/hello)"]:::controller
        authEntry["JwtAuthenticationEntryPoint"]:::security

        client -->|"GET_/hello(withJWT)"| filter
        filter -->|"ifValid:validateToken"| jwtUtil
        filter -->|"ifValid"| helloCtrl
        helloCtrl -->|"Response:Hello"| client
        filter -->|"ifInvalid"| authEntry
        authEntry -->|"401_Unauthorized"| client
    end

    %% Security Configuration
    subgraph "Security Configuration"
        webSec["WebSecurityConfig"]:::security
        webSec --> jwtUtil
        webSec --> filter
        webSec --> authEntry
    end

    %% Build & Configuration
    subgraph "Build & Configuration"
        pom["pom.xml"]:::build
        appProp["application.properties"]:::build
    end

    %% Styles
    classDef client fill:#ffefc6,stroke:#b58900,stroke-width:2px;
    classDef controller fill:#cce5ff,stroke:#268bd2,stroke-width:2px;
    classDef service fill:#d0f0c0,stroke:#859900,stroke-width:2px;
    classDef security fill:#ffdce0,stroke:#d33682,stroke-width:2px;
    classDef model fill:#f0d9ff,stroke:#6c71c4,stroke-width:2px;
    classDef build fill:#e0ffff,stroke:#2aa198,stroke-width:2px;

    %% Click Events
    click authCtrl "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/controller/JwtAuthenticationController.java"
    click helloCtrl "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/controller/HelloWorldController.java"
    click webSec "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/config/WebSecurityConfig.java"
    click jwtUtil "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/config/JwtTokenUtil.java"
    click filter "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/config/JwtRequestFilter.java"
    click authEntry "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/config/JwtAuthenticationEntryPoint.java"
    click userSvc "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/service/JwtUserDetailsService.java"
    click jwtReq "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/model/JwtRequest.java"
    click jwtResp "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/java/com/ngttech/demo/model/JwtResponse.java"
    click pom "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/pom.xml"
    click appProp "https://github.com/narayangeed/spring-boot-jwt-demo/blob/main/src/main/resources/application.properties"
```
