# lochuung/simple-jwt-auth architecture

> Architecture diagram of the GitHub repository lochuung/simple-jwt-auth, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lochuung/simple-jwt-auth
- Repository: https://github.com/lochuung/simple-jwt-auth

## Overview

1. **Project type and runtime**
   - This is a single Spring Boot service implemented in Java and built with Maven.
   - It exposes a small HTTP API for authentication and token refresh, with Spring Security handling request protection.

2. **Top-level application entry point**
   - `SimpleJwtAuthApplication` is the bootstrap class.
   - `application.properties` holds runtime configuration, especially JWT expiration settings and server port.

3. **API surface**
   - `AuthenticationController` is the only visible controller in the tree.
   - It exposes two endpoints:
     - `POST /api/auth/login`
     - `POST /api/auth/refresh-token`
   - These endpoints are the external entry points into the authentication flow.

4. **Authentication flow**
   - Login starts with `AuthenticationRequest` and ends with `AuthenticationResponse`.
   - The controller delegates to `AuthenticationService`, which verifies credentials, creates tokens, and returns both access and refresh tokens.
   - The refresh endpoint uses the refresh token from the Authorization header to issue a new token pair.

5. **Security boundary**
   - `SecurityConfiguration` defines the Spring Security setup.
   - `CustomerUserDetailsService` likely bridges application users to Spring Security’s authentication model.
   - `JwtService` encapsulates token creation/validation and is the core security utility in the app.

6. **Domain model**
   - The persistence model is centered on `User`, `Role`, and `Privilege`.
   - This suggests role-based authorization, with privileges attached through roles rather than hardcoded permissions.

7. **Persistence layer**
   - `UserRepository`, `RoleRepository`, and `PrivilegeRepository` form the data access boundary.
   - The app uses Spring Data JPA, so the database is accessed through repository abstractions rather than explicit SQL in the codebase.

8. **Service layer**
   - `UserService` and `UserServiceImpl` provide user-related business operations outside authentication.
   - `AuthenticationService` is the main orchestration service for login and token refresh.
   - This keeps HTTP concerns in the controller and security/token logic in the service layer.

9. **DTO and mapping layer**
   - The `dto` package separates API contracts from JPA entities.
   - `UserMapper`, `RoleMapper`, and `PrivilegeMapper` convert between entities and DTOs, reducing direct exposure of persistence objects to the API.

10. **Error handling**
   - `BadRequestException` and `UnauthorizedException` represent the main failure modes.
   - `CustomControllerAdvice` centralizes exception-to-response translation, so authentication and validation failures are handled consistently.

11. **Likely end-to-end data flow**
   - Client calls login endpoint → controller → authentication service → user lookup via repository → token generation via JWT service → response with access/refresh tokens.
   - For refresh, the client sends the refresh token in the Authorization header → controller/service validates it → new tokens are returned.

12. **Tests and scope**
   - The test tree is minimal, indicating the repository is mainly a reference implementation rather than a large application suite.
   - Overall, the architecture is a compact layered Spring Security/JPA authentication service with a clear boundary between API, security, service, and persistence concerns.

Main entry points: [`src/main/java/com/huuloc/simplejwtauth/service/auth/AuthenticationService.java`](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/auth/AuthenticationService.java) (Auth service), [`src/main/java/com/huuloc/simplejwtauth/controller/AuthenticationController.java`](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/controller/AuthenticationController.java) (Auth API), [`src/main/java/com/huuloc/simplejwtauth/entity/Role.java`](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/entity/Role.java) (Role), [`src/main/java/com/huuloc/simplejwtauth/repository/UserRepository.java`](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/repository/UserRepository.java) (User repo)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App"]
  node_app_main(("App main<br/>Spring Boot entrypoint"))
  node_app_props["Config<br/>runtime properties"]
end

subgraph group_api["API"]
  node_api_auth_controller["Auth API<br/>REST controller"]
  node_api_auth_req["Login request<br/>auth DTO"]
  node_api_auth_res["Auth response<br/>auth DTO"]
end

subgraph group_security["Security"]
  node_sec_config["Security config<br/>Spring Security setup"]
  node_sec_auth_service["Auth service<br/>authentication orchestration"]
  node_sec_jwt["JWT service<br/>token utility<br/>[JwtService.java]"]
  node_sec_user_details["User details<br/>security adapter"]
end

subgraph group_domain["Domain"]
  node_domain_user[("User<br/>JPA entity<br/>[User.java]")]
  node_domain_role[("Role<br/>JPA entity<br/>[Role.java]")]
  node_domain_priv[("Privilege<br/>JPA entity<br/>[Privilege.java]")]
  node_map_user["User mapper<br/>DTO mapper<br/>[UserMapper.java]"]
  node_map_role["Role mapper<br/>DTO mapper<br/>[RoleMapper.java]"]
  node_map_priv["Privilege mapper<br/>DTO mapper"]
end

subgraph group_persistence["Persistence"]
  node_repo_user[("User repo<br/>Spring Data repository")]
  node_repo_role[("Role repo<br/>Spring Data repository")]
  node_repo_priv[("Privilege repo<br/>Spring Data repository")]
end

subgraph group_service["Services"]
  node_user_service["User service<br/>business API<br/>[UserService.java]"]
  node_user_service_impl["User service impl<br/>service implementation"]
end

subgraph group_error["Errors"]
  node_err_advice["Controller advice<br/>exception handler"]
  node_err_bad_req(("Bad request<br/>domain exception"))
  node_err_unauth(("Unauthorized<br/>domain exception"))
end

node_app_main -->|"loads"| node_sec_config
node_app_main -->|"reads"| node_app_props
node_api_auth_controller -->|"accepts"| node_api_auth_req
node_api_auth_controller -->|"returns"| node_api_auth_res
node_api_auth_controller -->|"delegates"| node_sec_auth_service
node_api_auth_controller -.->|"handled by"| node_err_advice
node_sec_config -->|"wires"| node_sec_user_details
node_sec_config -->|"uses"| node_sec_jwt
node_sec_auth_service -->|"authenticates via"| node_sec_user_details
node_sec_auth_service -->|"issues tokens"| node_sec_jwt
node_sec_auth_service -->|"looks up"| node_repo_user
node_sec_auth_service -->|"throws"| node_err_unauth
node_sec_auth_service -->|"throws"| node_err_bad_req
node_sec_user_details -->|"loads"| node_repo_user
node_repo_user -->|"persists"| node_domain_user
node_repo_role -->|"persists"| node_domain_role
node_repo_priv -->|"persists"| node_domain_priv
node_domain_user -->|"has"| node_domain_role
node_domain_role -->|"grants"| node_domain_priv
node_user_service_impl -->|"implements"| node_user_service
node_user_service_impl -->|"uses"| node_repo_user
node_user_service_impl -->|"maps"| node_map_user
node_map_user -.->|"transforms"| node_domain_user
node_map_role -.->|"transforms"| node_domain_role
node_map_priv -.->|"transforms"| node_domain_priv
node_err_advice -->|"handles"| node_err_bad_req
node_err_advice -->|"handles"| node_err_unauth

click node_app_main "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/SimpleJwtAuthApplication.java"
click node_app_props "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/resources/application.properties"
click node_api_auth_controller "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/controller/AuthenticationController.java"
click node_api_auth_req "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/auth/AuthenticationRequest.java"
click node_api_auth_res "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/auth/AuthenticationResponse.java"
click node_sec_config "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/configuration/SecurityConfiguration.java"
click node_sec_auth_service "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/auth/AuthenticationService.java"
click node_sec_jwt "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/auth/JwtService.java"
click node_sec_user_details "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/auth/CustomerUserDetailsService.java"
click node_domain_user "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/entity/User.java"
click node_domain_role "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/entity/Role.java"
click node_domain_priv "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/entity/Privilege.java"
click node_map_user "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/mapper/UserMapper.java"
click node_map_role "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/mapper/RoleMapper.java"
click node_map_priv "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/mapper/PrivilegeMapper.java"
click node_repo_user "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/repository/UserRepository.java"
click node_repo_role "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/repository/RoleRepository.java"
click node_repo_priv "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/repository/PrivilegeRepository.java"
click node_user_service "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/UserService.java"
click node_user_service_impl "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/impl/UserServiceImpl.java"
click node_err_advice "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/exception/advice/CustomControllerAdvice.java"
click node_err_bad_req "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/exception/BadRequestException.java"
click node_err_unauth "https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/exception/UnauthorizedException.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_main,node_app_props,node_err_advice,node_err_bad_req,node_err_unauth toneBlue
class node_api_auth_controller,node_api_auth_req,node_api_auth_res toneAmber
class node_sec_config,node_sec_auth_service,node_sec_jwt,node_sec_user_details toneMint
class node_domain_user,node_domain_role,node_domain_priv,node_map_user,node_map_role,node_map_priv toneRose
class node_repo_user,node_repo_role,node_repo_priv toneIndigo
class node_user_service,node_user_service_impl toneTeal
```

## Components

### App

Bootstrap and runtime config

- [App main](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/SimpleJwtAuthApplication.java): Spring Boot entrypoint · Application bootstrap class · `src/main/java/com/huuloc/simplejwtauth/SimpleJwtAuthApplication.java`
- [Config](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/resources/application.properties): runtime properties · Server and JWT settings · `src/main/resources/application.properties`

### API

HTTP entry points and request/response contracts

- [Auth API](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/controller/AuthenticationController.java): REST controller · Login and refresh-token endpoints · `src/main/java/com/huuloc/simplejwtauth/controller/AuthenticationController.java`
- [Login request](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/auth/AuthenticationRequest.java): auth DTO · Credentials submitted to login · `src/main/java/com/huuloc/simplejwtauth/dto/auth/AuthenticationRequest.java`
- [Auth response](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/auth/AuthenticationResponse.java): auth DTO · Access and refresh token response · `src/main/java/com/huuloc/simplejwtauth/dto/auth/AuthenticationResponse.java`

### Security

Authentication, token handling, and Spring Security setup

- [Security config](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/configuration/SecurityConfiguration.java): Spring Security setup · Request protection and security wiring · `src/main/java/com/huuloc/simplejwtauth/configuration/SecurityConfiguration.java`
- [Auth service](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/auth/AuthenticationService.java): authentication orchestration · Verifies credentials and issues tokens · `src/main/java/com/huuloc/simplejwtauth/service/auth/AuthenticationService.java`
- [JWT service](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/auth/JwtService.java): token utility · Creates and validates JWTs · `src/main/java/com/huuloc/simplejwtauth/service/auth/JwtService.java`
- [User details](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/auth/CustomerUserDetailsService.java): security adapter · Bridges users to Spring Security · `src/main/java/com/huuloc/simplejwtauth/service/auth/CustomerUserDetailsService.java`

### Domain

Core entities and data mapping

- [User](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/entity/User.java): JPA entity · Application user model · `src/main/java/com/huuloc/simplejwtauth/entity/User.java`
- [Role](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/entity/Role.java): JPA entity · Role model for authorization · `src/main/java/com/huuloc/simplejwtauth/entity/Role.java`
- [Privilege](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/entity/Privilege.java): JPA entity · Atomic privilege model · `src/main/java/com/huuloc/simplejwtauth/entity/Privilege.java`
- [User mapper](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/mapper/UserMapper.java): DTO mapper · Maps user entities to DTOs · `src/main/java/com/huuloc/simplejwtauth/dto/mapper/UserMapper.java`
- [Role mapper](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/mapper/RoleMapper.java): DTO mapper · Maps role entities to DTOs · `src/main/java/com/huuloc/simplejwtauth/dto/mapper/RoleMapper.java`
- [Privilege mapper](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/dto/mapper/PrivilegeMapper.java): DTO mapper · Maps privilege entities to DTOs · `src/main/java/com/huuloc/simplejwtauth/dto/mapper/PrivilegeMapper.java`

### Persistence

Repository boundary

- [User repo](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/repository/UserRepository.java): Spring Data repository · User persistence boundary · `src/main/java/com/huuloc/simplejwtauth/repository/UserRepository.java`
- [Role repo](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/repository/RoleRepository.java): Spring Data repository · Role persistence boundary · `src/main/java/com/huuloc/simplejwtauth/repository/RoleRepository.java`
- [Privilege repo](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/repository/PrivilegeRepository.java): Spring Data repository · Privilege persistence boundary · `src/main/java/com/huuloc/simplejwtauth/repository/PrivilegeRepository.java`

### Services

Business orchestration and user operations

- [User service](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/UserService.java): business API · User-related business operations · `src/main/java/com/huuloc/simplejwtauth/service/UserService.java`
- [User service impl](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/service/impl/UserServiceImpl.java): service implementation · Concrete user business logic · `src/main/java/com/huuloc/simplejwtauth/service/impl/UserServiceImpl.java`

### Errors

Exception handling and response translation

- [Controller advice](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/exception/advice/CustomControllerAdvice.java): exception handler · Maps exceptions to HTTP responses · `src/main/java/com/huuloc/simplejwtauth/exception/advice/CustomControllerAdvice.java`
- [Bad request](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/exception/BadRequestException.java): domain exception · Input or state validation failure · `src/main/java/com/huuloc/simplejwtauth/exception/BadRequestException.java`
- [Unauthorized](https://github.com/lochuung/simple-jwt-auth/blob/main/src/main/java/com/huuloc/simplejwtauth/exception/UnauthorizedException.java): domain exception · Authentication or token failure · `src/main/java/com/huuloc/simplejwtauth/exception/UnauthorizedException.java`

## Connections

- App main → Security config: loads · Bootstraps security configuration
- App main → Config: reads · Uses runtime configuration
- Auth API → Login request: accepts · Login request payload
- Auth API → Auth response: returns · Authentication result payload
- Auth API → Auth service: delegates · Login and refresh orchestration
- Auth API → Controller advice: handled by · Exceptions translated centrally
- Security config → User details: wires · User lookup for security
- Security config → JWT service: uses · JWT validation and signing
- Auth service → User details: authenticates via · Loads security principal data
- Auth service → JWT service: issues tokens · Creates access and refresh tokens
- Auth service → User repo: looks up · Finds users during login
- Auth service → Unauthorized: throws · Invalid credentials or token
- Auth service → Bad request: throws · Invalid request state
- User details → User repo: loads · Retrieves application user records
- User repo → User: persists · User entity storage
- Role repo → Role: persists · Role entity storage
- Privilege repo → Privilege: persists · Privilege entity storage
- User → Role: has · Users are associated with roles
- Role → Privilege: grants · Roles carry privileges
- User service impl → User service: implements · Concrete user operations
- User service impl → User repo: uses · User data access
- User service impl → User mapper: maps · Converts user entities and DTOs
- User mapper → User: transforms · Entity conversion boundary
- Role mapper → Role: transforms · Entity conversion boundary
- Privilege mapper → Privilege: transforms · Entity conversion boundary
- Controller advice → Bad request: handles · Bad request response mapping
- Controller advice → Unauthorized: handles · Unauthorized response mapping
