How lochuung/simple-jwt-auth works
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: AuthenticationService.java Auth service · AuthenticationController.java Auth API · Role.java Role · UserRepository.java User repo
Components 23
App
Bootstrap and runtime config
API
HTTP entry points and request/response contracts
- Auth API · Login and refresh-token endpoints
src/main/java/com/huuloc/simplejwtauth/controller/AuthenticationController.java - Login request · Credentials submitted to login
src/main/java/com/huuloc/simplejwtauth/dto/auth/AuthenticationRequest.java - Auth response · 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 · Request protection and security wiring
src/main/java/com/huuloc/simplejwtauth/configuration/SecurityConfiguration.java - Auth service · Verifies credentials and issues tokens
src/main/java/com/huuloc/simplejwtauth/service/auth/AuthenticationService.java - JWT service · Creates and validates JWTs
src/main/java/com/huuloc/simplejwtauth/service/auth/JwtService.java - User details · Bridges users to Spring Security
src/main/java/com/huuloc/simplejwtauth/service/auth/CustomerUserDetailsService.java
Domain
Core entities and data mapping
- User · Application user model
src/main/java/com/huuloc/simplejwtauth/entity/User.java - Role · Role model for authorization
src/main/java/com/huuloc/simplejwtauth/entity/Role.java - Privilege · Atomic privilege model
src/main/java/com/huuloc/simplejwtauth/entity/Privilege.java - User mapper · Maps user entities to DTOs
src/main/java/com/huuloc/simplejwtauth/dto/mapper/UserMapper.java - Role mapper · Maps role entities to DTOs
src/main/java/com/huuloc/simplejwtauth/dto/mapper/RoleMapper.java - Privilege mapper · Maps privilege entities to DTOs
src/main/java/com/huuloc/simplejwtauth/dto/mapper/PrivilegeMapper.java
Persistence
Repository boundary
- User repo · User persistence boundary
src/main/java/com/huuloc/simplejwtauth/repository/UserRepository.java - Role repo · Role persistence boundary
src/main/java/com/huuloc/simplejwtauth/repository/RoleRepository.java - Privilege repo · Privilege persistence boundary
src/main/java/com/huuloc/simplejwtauth/repository/PrivilegeRepository.java
Services
Business orchestration and user operations
- User service · User-related business operations
src/main/java/com/huuloc/simplejwtauth/service/UserService.java - User service impl · Concrete user business logic
src/main/java/com/huuloc/simplejwtauth/service/impl/UserServiceImpl.java
Errors
Exception handling and response translation
- Controller advice · Maps exceptions to HTTP responses
src/main/java/com/huuloc/simplejwtauth/exception/advice/CustomControllerAdvice.java - Bad request · Input or state validation failure
src/main/java/com/huuloc/simplejwtauth/exception/BadRequestException.java - Unauthorized · Authentication or token failure
src/main/java/com/huuloc/simplejwtauth/exception/UnauthorizedException.java
Connections 27
- 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