# bezkoder/spring-boot-spring-security-jwt-authentication architecture

> Architecture diagram of the GitHub repository bezkoder/spring-boot-spring-security-jwt-authentication, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bezkoder/spring-boot-spring-security-jwt-authentication
- Repository: https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication

## Overview

##### Project Type Analysis
This is a Spring Boot backend application focused on JWT-based authentication and authorization, with potential to be part of a full-stack application. The project implements security features using Spring Security and JPA for data persistence.

##### Main Components

A. Client Layer (External)
- Web Browsers
- Frontend Applications (Angular/React/Vue clients)

B. API Layer
- AuthController (Authentication endpoints)
- TestController (Protected resource endpoints)

C. Security Layer
- JWT Authentication Filter
- Security Configuration
- JWT Utils
- Authentication Entry Point

D. Service Layer
- UserDetailsService
- User Management Services

E. Repository Layer
- UserRepository
- RoleRepository

F. Database Layer
- User Table
- Role Table

- [Client Layer]
- ↓
- [API Layer]
- ↓
- [Security Layer]
- ↓
- [Service Layer]
- ↓
- [Repository Layer]
- ↓
- [Database Layer]

- Client → AuthController → Security Layer → UserDetailsService → Database
- Client → JWT Filter → Protected Resources → Service Layer → Database

##### Specific Implementation Details to Highlight

a) Security Components:
- AuthTokenFilter
- JwtUtils
- WebSecurityConfig
- AuthEntryPointJwt

b) Data Models:
- User
- Role (with ERole enum)

c) Request/Response Objects:
- LoginRequest
- SignupRequest
- JwtResponse
- MessageResponse

- [Login] → [Generate JWT] → [Store Token]
- [Protected Request] → [Validate JWT] → [Process Request]

- Users (id, username, email, password)
- Roles (id, name)
- User_Roles (user_id, role_id)

##### Notes Section
Important configuration details:
- JWT expiration time
- Security constraints
- Database configuration options
- Available roles (ROLE_USER, ROLE_MODERATOR, ROLE_ADMIN)

Remember to indicate the stateless nature of the JWT implementation and how it integrates with Spring Security's authentication mechanisms.

This system design should give a clear understanding of both the security architecture and the application's overall structure while maintaining readability and technical accuracy.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph CL[Client Layer]
        Browser["Web Browser"]
        Frontend["Frontend Application"]
    end

    %% API Layer
    subgraph AL[API Layer]
        AuthC["Authentication Controller"]:::public
        TestC["Test Controller"]:::protected
    end

    %% Security Layer
    subgraph SL[Security Layer]
        JWT["JWT Utils"]:::security
        AuthFilter["Auth Token Filter"]:::security
        SecConfig["Security Config"]:::security
        AuthEntry["Auth Entry Point"]:::security
    end

    %% Service Layer
    subgraph SVL[Service Layer]
        UDS["UserDetailsService"]:::service
        UDI["UserDetailsImpl"]:::service
    end

    %% Repository Layer
    subgraph RL[Repository Layer]
        UserRepo["User Repository"]:::repo
        RoleRepo["Role Repository"]:::repo
    end

    %% Database Layer
    subgraph DB[Database Layer]
        Users[(Users)]:::database
        Roles[(Roles)]:::database
        UserRoles[(User_Roles)]:::database
    end

    %% Data Models and DTOs
    subgraph Models[Data Models]
        User["User Model"]:::model
        Role["Role Model"]:::model
        ERole["Role Enum"]:::model
    end

    subgraph DTOs[Request/Response DTOs]
        LoginReq["Login Request"]:::dto
        SignupReq["Signup Request"]:::dto
        JwtRes["JWT Response"]:::dto
        MsgRes["Message Response"]:::dto
    end

    %% Relationships
    Browser --> Frontend
    Frontend --> AuthC & TestC
    AuthC --> JWT
    AuthC --> UDS
    TestC --> AuthFilter
    AuthFilter --> JWT
    JWT --> UDS
    UDS --> UDI
    UDS --> UserRepo
    UserRepo --> Users
    RoleRepo --> Roles
    Users --> UserRoles
    Roles --> UserRoles

    %% Styling
    classDef public fill:#90EE90
    classDef protected fill:#FFE4B5
    classDef security fill:#ADD8E6
    classDef service fill:#DDA0DD
    classDef repo fill:#F0F8FF
    classDef database fill:#DCDCDC
    classDef model fill:#98FB98
    classDef dto fill:#FFF0F5

    %% Click Events
    click AuthC "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/controllers/AuthController.java"
    click TestC "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/controllers/TestController.java"
    click JWT "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/jwt/JwtUtils.java"
    click AuthFilter "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/jwt/AuthTokenFilter.java"
    click SecConfig "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java"
    click AuthEntry "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/jwt/AuthEntryPointJwt.java"
    click UDS "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsServiceImpl.java"
    click UDI "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsImpl.java"
    click UserRepo "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/repository/UserRepository.java"
    click RoleRepo "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/repository/RoleRepository.java"
    click User "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/models/User.java"
    click Role "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/models/Role.java"
    click ERole "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/models/ERole.java"
    click LoginReq "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/payload/request/LoginRequest.java"
    click SignupReq "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/payload/request/SignupRequest.java"
    click JwtRes "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/payload/response/JwtResponse.java"
    click MsgRes "https://github.com/bezkoder/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/payload/response/MessageResponse.java"
```
