# hieu-pt/spring-boot-spring-security-jwt-authentication architecture

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

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

## Overview

##### Project type and purpose
- This is a backend-only Spring Boot REST API implementing JWT-based authentication and authorization with Spring Security and Spring Data JPA.
- The README describes user registration, login, role-based authorization flows, and refresh token support.
- There is no frontend in this repo; clients (web/mobile) consume the exposed REST endpoints.

##### File structure
- Top-level:
- pom.xml, Maven wrapper files, README.md
- src/main/java/com/bezkoder/springjwt – application code
- src/main/resources/application.properties – datasource and JWT config
- src/test/java/com/bezkoder/springjwt – basic smoke tests
- Under src/main/java/com/bezkoder/springjwt:
- controllers – REST controllers (AuthController, TestController)
- models – JPA entities (User, Role, ERole enum)
- payload – DTOs for requests (LoginRequest, SignupRequest) and responses (JwtResponse, MessageResponse)
- repository – Spring Data JPA repositories (UserRepository, RoleRepository)
- security –
­– WebSecurityConfig: HTTP security rules
­– jwt sub‐package: JwtUtils, AuthTokenFilter, AuthEntryPointJwt
­– services: UserDetailsServiceImpl, UserDetailsImpl
- No external config besides application.properties; no microservices or frontend modules.

##### README for additional insights
- Authentication flow (login/signup → JWT generation → protected endpoints)
- Overall server architecture (security layer wrapping controllers, services, repository, database)

a. Main components
- Client(s) (Web/Mobile)
- Spring Boot Application
- Controller Layer (AuthController, TestController)
- Security Layer
· WebSecurityConfig
· AuthTokenFilter
· AuthEntryPointJwt
· JwtUtils
- Service Layer (UserDetailsServiceImpl / UserDetailsImpl)
- Repository Layer (UserRepository, RoleRepository)
- Domain Models (User, Role)
- Relational Database (MySQL/PostgreSQL)

b. Relationships and interactions
1. Client → HTTP POST /api/auth/signup or /login → AuthController
2. AuthController uses UserRepository/RoleRepository to persist or fetch users
3. On login, AuthController calls AuthenticationManager (configured in WebSecurityConfig), which delegates to UserDetailsServiceImpl → loads user via UserRepository
4. If credentials valid, JwtUtils generates token → returned in JwtResponse
5. For protected endpoints (/api/test/**), every request passes through AuthTokenFilter (registered in WebSecurityConfig)
­– AuthTokenFilter uses JwtUtils to validate token, set SecurityContext
6. Controller methods annotated with @PreAuthorize or standard role checks
7. Data persisted/retrieved via Spring Data JPA to the Database

c. Architectural patterns and design principles
- Layered architecture (Controller → Service → Repository)
- Spring Security filter chain for cross-cutting concerns (authentication, exception handling)
- Stateless authentication via JWT
- Dependency inversion: controllers depend on interfaces (UserDetailsService), Spring injects implementations

d. Key technologies
- Spring Boot
- Spring Security
- JSON Web Token (JWT)
- Spring Data JPA (Hibernate)
- PostgreSQL or MySQL (configurable)
- Maven build system

##### Essential elements to capture
- Authentication flow for login and token refresh
- Security filter chain before hitting business logic
- Role-based access decision after token validation
- Persistence layer interactions
- Configuration entry point (WebSecurityConfig) that ties filters into the HTTP request pipeline

## Diagram (Mermaid)

```mermaid
flowchart TB
    Client["Client (Web/Mobile)"]:::external

    subgraph "SpringBootSecurityJwtApplication"
        direction TB
        App["SpringBootSecurityJwtApplication"]:::app

        subgraph "Security Layer"
            direction TB
            WebSecurityConfig["WebSecurityConfig"]:::security
            AuthTokenFilter["AuthTokenFilter"]:::security
            AuthEntryPointJwt["AuthEntryPointJwt"]:::security
            JwtUtils["JwtUtils"]:::security
        end

        subgraph "Controller Layer"
            direction TB
            AuthController["AuthController"]:::controller
            TestController["TestController"]:::controller
        end

        subgraph "Service Layer"
            direction TB
            UserDetailsServiceImpl["UserDetailsServiceImpl"]:::service
            UserDetailsImpl["UserDetailsImpl"]:::service
        end

        subgraph "Repository Layer"
            direction TB
            UserRepository["UserRepository"]:::repository
            RoleRepository["RoleRepository"]:::repository
        end

        subgraph "Domain Models"
            direction TB
            User["User"]:::model
            Role["Role"]:::model
            ERole["ERole"]:::model
        end
    end

    Database["Relational Database"]:::db

    Client -->|"HTTP Request"| WebSecurityConfig
    WebSecurityConfig --> AuthTokenFilter
    AuthTokenFilter --> JwtUtils
    AuthTokenFilter --> AuthEntryPointJwt
    AuthTokenFilter --> AuthController
    AuthController -->|"calls"| UserDetailsServiceImpl
    TestController -->|"calls"| UserDetailsServiceImpl
    UserDetailsServiceImpl -->|"loads user"| UserRepository
    UserDetailsServiceImpl -->|"loads role"| RoleRepository
    UserRepository -->|"JPA Query"| Database
    RoleRepository -->|"JPA Query"| Database

    click App "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplication.java"
    click AuthController "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/controllers/AuthController.java"
    click TestController "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/controllers/TestController.java"
    click WebSecurityConfig "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java"
    click AuthEntryPointJwt "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/jwt/AuthEntryPointJwt.java"
    click AuthTokenFilter "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/jwt/AuthTokenFilter.java"
    click JwtUtils "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/jwt/JwtUtils.java"
    click UserDetailsServiceImpl "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsServiceImpl.java"
    click UserDetailsImpl "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsImpl.java"
    click UserRepository "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/repository/UserRepository.java"
    click RoleRepository "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/repository/RoleRepository.java"
    click User "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/models/User.java"
    click Role "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/models/Role.java"
    click ERole "https://github.com/hieu-pt/spring-boot-spring-security-jwt-authentication/blob/master/src/main/java/com/bezkoder/springjwt/models/ERole.java"

    classDef external fill:#cccccc,stroke:#333,stroke-width:1px
    classDef app fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef security fill:#a2d5c6,stroke:#333,stroke-width:1px
    classDef controller fill:#b3cde0,stroke:#333,stroke-width:1px
    classDef service fill:#fbb4ae,stroke:#333,stroke-width:1px
    classDef repository fill:#fed9a6,stroke:#333,stroke-width:1px
    classDef model fill:#decbe4,stroke:#333,stroke-width:1px
    classDef db fill:#f2f2f2,stroke:#333,stroke-width:1px
```
