Modern implementation of Spring Security based on Clean/Hexagonal Architecture, using JWT with RSA-256 asymmetric encryption.
- Framework: Spring Boot 3.5.13 (Spring 6.3)
- Java: 21
- Security: Spring Security 6.x (OAuth2 Resource Server, JWT RS256)
- Database: PostgreSQL 15+
- Mapping: MapStruct 1.6.3
- Utils: Lombok
- Architecture: Hexagonal (Ports & Adapters) — 100% desacoplada
TokenGeneratorPort: Dominio sin dependencias de infraestructuraUserResponseDTO: El password jamás se expone al cliente- Excepciones de dominio con HTTP semántico (409 Conflict, 403 Forbidden, 401 Unauthorized)
UserEntitycon patrón@Builder— inmutable post-construcciónAppSecurityPropertiesrecord tipado para toda la configuración de seguridad@Transactionalen operaciones de escritura para garantía de atomicidad
src/main/java/org/zgo/security/
├── domain/ # CORE BUSINESS LOGIC (Framework-independent)
│ ├── model/ # Domain Records (User, Role)
│ └── ports/ # Port Interfaces
├── application/ # APPLICATION SERVICES
│ └── service/ # Use Case Implementations
└── infrastructure/ # TECHNICAL DETAILS (Adapters & Config)
├── adapter/ # Infrastructure Implementations
└── config/ # Security and Spring Bean configuration
-
Start Database:
docker-compose up -d
-
Set JAVA_HOME (solo para JDK 21): Asegúrate de apuntar la variable de entorno a tu instalación de Java 21 antes de ejecutar:
$env:JAVA_HOME="C:\Java\jdk-21"
-
Compile and Run using Maven Wrapper: Se ha generado un Maven Wrapper (
mvnw) para asegurar que el proyecto se corra siempre con la misma configuración de Maven en cualquier entorno.En Windows (PowerShell/CMD):
.\mvnw.cmd clean spring-boot:run
En Linux/Mac:
./mvnw clean spring-boot:run
- JWT Signing: Uses RS256 (RSA with SHA-256).
- Public/Private Keys: Stored in
src/main/resources/certs/. - User Passwords: Encrypted with BCrypt.
- Exception Handling: Global
@RestControllerAdvicemapping domain/security errors to HTTP 4x/5x.
Import the collection located in: postman/SpringSecurity_Collection.json
- Register:
POST /api/v1/auth/register - Login:
POST /api/v1/auth/login(Returns Bearer token) - Protected Resources: Add
Authorization: Bearer <token>header.