Skip to content

This project is a User Service built using Hexagonal Architecture with the Spring Framework. It follows a modular and maintainable design, separating business logic from infrastructure concerns while ensuring security and scalability.

Notifications You must be signed in to change notification settings

AndreaLlerena2003/user-service-hexagonal-spring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

User Service Architecture

Overview

This service is responsible for user management and authentication. It follows a clean/hexagonal architecture pattern with clear separation of concerns.

Project Structure

user_service/
├── src/
│   └── main/
│       └── java/
│           └── microservices/
│               └── user_service/
│                   ├── application/
│                   │   └── services/
│                   │       ├── auth/
│                   │       │   └── AuthService.java
│                   │       └── user/
│                   │           └── UserService.java
│                   ├── domain/
│                   │   ├── dto/
│                   │   │   ├── auth/
│                   │   │   │   ├── AuthRequestDTO.java
│                   │   │   │   ├── AuthResponseDTO.java
│                   │   │   │   └── RegisterRequest.java
│                   │   │   └── user/
│                   │   ├── exception/
│                   │   │   └── UserNotFoundException.java
│                   │   ├── mapper/
│                   │   │   └── UserMapper.java
│                   │   └── model/
│                   ├── infrastructure/
│                   │   ├── adapters/
│                   │   │   ├── persistence/
│                   │   │   │   ├── entity/
│                   │   │   │   │   ├── AuthEntity.java
│                   │   │   │   │   ├── UserEntity.java
│                   │   │   │   │   └── UserRol.java
│                   │   │   │   ├── mappers/
│                   │   │   │   │   ├── AuthPersistenceMapper.java
│                   │   │   │   │   └── UserPersistenceMapper.java
│                   │   │   │   ├── repository/
│                   │   │   │   │   ├── jpa/
│                   │   │   │   │   │   ├── AuthJpaRepository.java
│                   │   │   │   │   │   └── UserJpaRepository.java
│                   │   │   │   │   ├── AuthRepositoryAdapter.java
│                   │   │   │   │   └── UserRepositoryAdapter.java
│                   │   │   └── rest/
│                   │   │       └── controller/
│                   │   │           ├── auth/
│                   │   │           │   └── AuthController.java
│                   │   │           ├── exception/
│                   │   │           │   └── GlobalExceptionHandler.java
│                   │   │           ├── responses/
│                   │   │           │   └── ApiResponse.java
│                   │   │           ├── security/
│                   │   │           │   ├── jwt/
│                   │   │           │   │   ├── JwtAuthenticationFilter.java
│                   │   │           │   │   ├── JwtService.java
│                   │   │           │   │   └── JwtValidationException.java
│                   │   │           │   └── SecurityConfig.java
│                   │   │           └── user/
│                   │   │               └── UserController.java
│                   │   └── security/
│                   │       └── jwt/
│                   │           ├── JwtAuthenticationFilter.java
│                   │           ├── JwtService.java
│                   │           ├── JwtValidationException.java
│                   │           └── SecurityConfig.java
│                   └── usecase/
│                       └── port/
│                           ├── in/
│                           │   ├── AuthServicePort.java
│                           │   └── UserServicePort.java
│                           └── out/
│                               ├── AuthRepositoryPort.java
│                               └── UserRepositoryPort.java
└── UserServiceApplication.java
├── resources/
└── test/

Architectural Layers

Domain Layer

Contains the core business logic and entities:

  • dto: Data Transfer Objects for API requests and responses
  • model: Domain entities
  • exception: Custom exceptions like UserNotFoundException
  • mapper: Object mappers like UserMapper for converting between entities and DTOs

Use Case / Port Layer

Defines the interfaces for the application's use cases:

  • port/in: Inbound port interfaces used by the application services
  • port/out: Outbound port interfaces that application services use to interact with repositories

Application Layer

Contains the implementation of use cases:

  • services/auth: Authentication-related business logic
  • services/user: User management business logic

Infrastructure Layer

Handles external concerns:

Persistence

  • entity: Database entities (UserEntity, AuthEntity, UserRol)
  • mappers: Persistence mappers (UserPersistenceMapper, AuthPersistenceMapper)
  • repository/jpa: JPA repositories (UserJpaRepository, AuthJpaRepository)
  • repository: Repository adapters (UserRepositoryAdapter, AuthRepositoryAdapter)

REST Controllers

  • controller/auth: Authentication controller
  • controller/user: User management controller
  • controller/exception: Global exception handling through GlobalExceptionHandler
  • controller/responses: Standardized API responses via ApiResponse

Security

  • security/jwt: JWT implementation components
    • JwtAuthenticationFilter.java: Intercepts requests to validate JWT tokens
    • JwtService.java: Handles JWT token generation and validation
    • JwtValidationException.java: Custom exception for JWT validation errors
  • SecurityConfig.java: Security configuration with Spring Security

Key Components

Authentication

  • AuthController: Handles authentication and registration endpoints
  • AuthService: Implements authentication logic
  • SecurityConfig: Configures security settings and JWT token validation
  • JwtService: Manages JWT token generation and validation

User Management

  • UserController: Exposes user management endpoints
  • UserService: Implements user management business logic
  • UserMapper: Converts between domain models and DTOs
  • UserNotFoundException: Custom exception for when users aren't found

Data Access

  • Entity Classes: Represent database tables (UserEntity, AuthEntity, UserRol)
  • JPA Repositories: Spring Data interfaces for database operations
  • Repository Adapters: Implement repository ports and use JPA repositories
  • Persistence Mappers: Convert between domain and persistence models

Security

  • JWT Authentication: Complete implementation of JWT-based authentication
    • Token generation and validation
    • Authentication filters
    • Custom exceptions for security issues
  • Security Configuration: Spring Security setup with appropriate authorization rules

API Endpoints

The service exposes REST APIs for:

  • User registration
  • User authentication
  • User management (CRUD operations)

Technology Stack

  • Java
  • Spring Boot
  • Spring Security
  • Spring Data JPA
  • JWT for authentication
  • Hexagonal Architecture pattern

About

This project is a User Service built using Hexagonal Architecture with the Spring Framework. It follows a modular and maintainable design, separating business logic from infrastructure concerns while ensuring security and scalability.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages