Camadas da Arquitetura - ecosif-auth

Visão Geral das Camadas

O projeto ecosif-auth segue a arquitetura em camadas do Spring Boot, com separação clara de responsabilidades.

Estrutura de Camadas

┌─────────────────────────────────────────────────────────────┐
│                    PRESENTATION LAYER                       │
│  ┌───────────────────────────────────────────────────────┐  │
│  │ Controllers                                           │  │
│  │ - AuthController                                      │  │
│  │ - DockerLogsController                                │  │
│  └───────────────────────────────────────────────────────┘  │
│  ┌───────────────────────────────────────────────────────┐  │
│  │ Filters                                               │  │
│  │ - TokenAuthenticationFilter                           │  │
│  └───────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                     BUSINESS LAYER                          │
│  ┌───────────────────────────────────────────────────────┐  │
│  │ Services                                              │  │
│  │ - UserService / UserServiceImpl                       │  │
│  │ - LocalUserDetailService                              │  │
│  │ - ExternalAuthenticationService                       │  │
│  │ - io.ecosif.security.jwt.TokenProvider (starter)      │  │
│  └───────────────────────────────────────────────────────┘  │
│  ┌───────────────────────────────────────────────────────┐  │
│  │ DTOs                                                  │  │
│  │ - LoginRequest                                        │  │
│  │ - LoginRequestAzure                                   │  │
│  │ - JwtAuthenticationResponse                           │  │
│  │ - UserInfo                                            │  │
│  │ - UserCreateAzureDTO                                  │  │
│  └───────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                      DATA ACCESS LAYER                      │
│  ┌───────────────────────────────────────────────────────┐  │
│  │ Repositories                                          │  │
│  │ - UserRepository                                      │  │
│  │ - RoleRepository                                      │  │
│  │ - BusinessAccountRepository                           │  │
│  └───────────────────────────────────────────────────────┘  │
│  ┌───────────────────────────────────────────────────────┐  │
│  │ Entities                                              │  │
│  │ - User                                                │  │
│  │ - Role                                                │  │
│  │ - BusinessAccount                                     │  │
│  └───────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                    INFRASTRUCTURE LAYER                     │
│  ┌───────────────────────────────────────────────────────┐  │
│  │ Database (PostgreSQL)                                 │  │
│  │ - Schema: public                                      │  │
│  │ - Tables: gr_user, roles, businessaccount             │  │
│  └───────────────────────────────────────────────────────┘  │
│  ┌───────────────────────────────────────────────────────┐  │
│  │ Configuration                                         │  │
│  │ - WebSecurityConfig                                   │  │
│  │ - PasswordEncoderConfig                               │  │
│  │ - OpenApiConfig                                       │  │
│  │ - AppProperties                                       │  │
│  └───────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘

1. Presentation Layer (Camada de Apresentação)

Controllers

Responsáveis por receber requisições HTTP, validar entradas e retornar respostas.

AuthController

DockerLogsController

Filters

TokenAuthenticationFilter

2. Business Layer (Camada de Negócio)

Services

Implementam a lógica de negócio do sistema.

UserService / UserServiceImpl

LocalUserDetailService

TokenProvider (starter)

DTOs (Data Transfer Objects)

Objetos para transferência de dados entre camadas.

LoginRequest

ExternalLoginRequest

LoginRequestAzure (removido)

JwtAuthenticationResponse

UserInfo

3. Data Access Layer (Camada de Acesso a Dados)

Repositories

Interfaces Spring Data JPA para acesso a dados.

UserRepository

RoleRepository

BusinessAccountRepository

Entities

Entidades JPA que representam tabelas do banco de dados.

User

Role

BusinessAccount

4. Infrastructure Layer (Camada de Infraestrutura)

Database

PostgreSQL

Flyway

Configuration

WebSecurityConfig

PasswordEncoderConfig

OpenApiConfig

AppProperties

Fluxo de Dados entre Camadas

Exemplo: Processo de Autenticação

1. Cliente → Controller
   POST /api/auth/signin
   { username, password }

2. Controller → Service
   userService.findUserByUsername(username)

3. Service → Repository
   userRepository.findByUsername(username)

4. Repository → Database
   SELECT * FROM gr_user WHERE username = ?

5. Database → Repository
   User entity

6. Repository → Service
   User entity

7. Service → Controller
   User entity

8. Controller → AuthenticationManager
   authenticate(username, password)

9. AuthenticationManager → LocalUserDetailService
   loadUserByUsername(username)

10. LocalUserDetailService → Repository → Database
    (busca usuário novamente)

11. AuthenticationManager → Controller
    Authentication (com LocalUser)

12. Controller → TokenProvider
    createToken(localUser)

13. TokenProvider → Controller
    JWT Token

14. Controller → Cliente
    200 OK + JWT Token + UserInfo

Princípios de Design

Separation of Concerns (Separação de Responsabilidades)

Dependency Inversion (Inversão de Dependências)

Single Responsibility (Responsabilidade Única)

Don't Repeat Yourself (DRY)

Testabilidade

A arquitetura em camadas facilita testes unitários e de integração: