Overview
The Authorization Service is built using Hexagonal Architecture (also known as Ports and Adapters), a pattern that promotes loose coupling and high testability by organizing code around the business domain.Architecture Layers
The system is organized into three main layers:1. Domain Layer (Core)
The domain layer contains the business logic and is completely independent of frameworks and external systems.Key Components:
Domain Models - Rich domain objects with business behavior:UserDomain(auth/domain/model/user/UserDomain.java:14)RoleDomain(auth/domain/model/role/RoleDomain.java:14)PermissionDomainActivityLogDomain(audit/domain/model/ActivityLogDomain.java)
UserId,UserEmail,UserPasswordRoleId,RoleName,RoleDescription- Located in
vopackages under each domain model
RoleAlreadyAssignedExceptionPermissionNotAssignedExceptionNullValueException
2. Application Layer
The application layer implements use cases by orchestrating domain objects and coordinating with external systems through ports. Services - Implement inbound ports:AuthService(auth/application/services/AuthService.java:18) - Handles login and authenticationAuditService(audit/application/services/AuditService.java:18) - Manages audit loggingUserService- CRUD operations for users
AuthResponse,UserResponse- Located in
application/dto/out
3. Adapter Layer
Adapters connect the application core to external systems.Inbound Adapters (Driving Side)
REST Controllers - Handle HTTP requests:adapter/in/web/controller/rest/:
AuthController(auth/adapter/in/web/controller/rest/AuthController.java:23)UserControllerRoleControllerPermissionController
AuditLogAspect(audit/adapter/in/aop/AuditLogAspect.java:27) - Intercepts methods for audit logging
Outbound Adapters (Driven Side)
Repository Adapters - Implement persistence ports:adapter/out/jpa/:
UserRepositoryAdapter(auth/adapter/out/jpa/UserRepositoryAdapter.java:27)RoleRepositoryAdapterPermissionRepositoryAdapterAuditRepositoryAdapter(audit/adapter/out/jpa/AuditRepositoryAdapter.java)
User,Role,Permission,ModuleActivityLog- Located in
adapter/out/jpa/entity/
UserJPAMapper- Converts betweenUserDomainandUserentityRoleJPAMapper,PermissionJPAMapperAuditMapper
Benefits of This Architecture
1. Technology Independence
The domain layer has zero dependencies on Spring, JPA, or any framework. This means:- Easy to test domain logic in isolation
- Can swap persistence or web frameworks without touching business logic
2. Clear Separation of Concerns
Each layer has a specific responsibility:- Domain: Business rules and invariants
- Application: Use case orchestration
- Adapters: Infrastructure and I/O
3. Testability
Ports can be mocked for testing:4. Flexibility
Multiple adapters can implement the same port:- JPA adapter for production
- In-memory adapter for testing
- REST client adapter for microservices
Data Flow Example
Let’s trace a user login request through the layers:- Inbound Adapter:
AuthControllerreceives HTTP POST to/api/auth/login - Application Service:
AuthService.login()orchestrates:- Calls
UserRepositoryPort.findByEmail()(outbound port) - Validates password using
PasswordEncoder - Calls
JwtUtil.generateToken()to create JWT
- Calls
- Outbound Adapter:
UserRepositoryAdapterqueries database:- Uses JPA
UserRepository.findByEmail() - Maps
Userentity toUserDomain
- Uses JPA
- Response: AuthService returns
AuthResponsewith token - Controller: Returns HTTP 200 with JSON response
Package Structure by Feature
The architecture is organized by feature (auth, audit) rather than by layer:- Understand all code related to a feature
- Extract features into separate microservices
- Work on features independently
Dependency Rules
Dependencies must point inward:- Domain has no dependencies (pure Java)
- Application depends only on Domain
- Adapters depend on Application and Domain
- Package-private visibility
- Dependency injection
- Interface segregation (ports)
Next Steps
Authentication
Learn how JWT authentication works
RBAC System
Understand role-based access control
Audit Logging
See how AOP-based auditing is implemented
API Reference
Explore the REST API endpoints