How ltt204/identity-service works
Project Type & Purpose
• The project is a microservice built using Clean Architecture principles. It serves as an identity management service handling authentication (JWT 2.0 with refresh token rotation), authorization, user management, and role-based access control.
• It is implemented using Java 17, Spring Boot, Spring Security, Spring Data JPA, along with MySQL and Redis. The README emphasizes robust security practices and the separation of concerns with clearly defined layers.
Key Components and Layers
• Domain Layer (Core):
• This layer hosts the enterprise business rules without dependency on outer layers.
• Contains core entities (User, Role, Permission, InvalidatedToken) within the "domain/entities" directory.
• Contains interfaces for repository access and core service definitions ("domain/repositories" and "domain/services").
• Application Layer:
• Implements application-specific business logic.
• Contains Data Transfer Objects (DTOs), service implementations (like AuthService, UserService, etc.), and adapters (repository adapters) that implement domain service requirements.
• Found under "src/main/java/org/ltt204/identityservice/application/".
• Infrastructure Layer:
• Provides implementations for persistence and external integrations.
• Contains JPA entities, repository implementations, mappers for converting between domain models and persistence models, and configuration setups (e.g., security, Redis, and application initialization).
• Located in "src/main/java/org/ltt204/identityservice/infra/".
• Presentation Layer:
• Exposes REST API endpoints such as AuthenticationController, UserController, RoleController, and PermissionController.
• Contains request and response DTOs for client communications, mappers to translate between internal models and external representations, and global exception handling.
• Found in "src/main/java/org/ltt204/identityservice/presentations/".
External Services & Dependencies
• MySQL Database:
• Acts as the data store. The schema is initialized via SQL scripts in "src/main/resources/db/migration".
• Redis:
• Used for token blacklisting (ensuring refresh token rotation) and caching aspects.
• JWT Authentication:
• Deployed via Spring Security configurations in the infrastructure layer, including custom JWT decoders and error handling mechanisms.
• Docker & Docker Compose:
• Simplify deployment for the database components and Redis. The presence of docker-compose.yml is an indicator of containerized deployment.
Relationship & Data Flow
• The controller receives the HTTP request, and using request DTOs and mappers, it forwards the request to the Application layer services.
• The Application layer orchestrates business operations and interacts with Domain layer interfaces which capture the core business rules.
• For persistence or external communications, adapters in the Application layer delegate operations to the Infrastructure layer (e.g., JPA repositories, Redis operations).
• Data then flows back from the Infrastructure through to the Application, which constructs response DTOs that are returned by the Presentation layer.
Architectural Patterns and Design Principles
• Clean Architecture:
• Separate concerns into distinct layers (Domain, Application, Infrastructure, Presentation). Emphasize minimal dependency from inner to outer layers.
• DTOs & Mappers:
• Use these design patterns to translate between domain models and external representations.
• Adapter Pattern:
• Repository adapters in the Application layer implement domain contract interfaces.
• Security Best Practices:
• JWT-based authentication, refresh token rotation, token blacklisting using Redis, and role-based authorization enforce security boundaries.