Overview
The API Gateway serves as the single entry point for all client requests to the Sistema de Ventas microservices architecture. Built with Spring Cloud Gateway, it provides intelligent routing, load balancing, authentication, and CORS configuration.Key Features
- Unified Entry Point: Single access point for all microservices
- Service Discovery: Integration with Eureka for dynamic routing
- Authentication Filter: JWT token validation for protected routes
- CORS Configuration: Cross-origin resource sharing for frontend integration
- Load Balancing: Automatic distribution across service instances
Architecture
Configuration
Server Setup
The gateway runs on port 8085 and connects to the Eureka service registry:jea-gateway-service.yml
Application Properties
The gateway connects to the Config Server for centralized configuration:application.yml
CORS Configuration
Global CORS settings allow the Angular frontend (port 4200) to communicate with the backend:jea-gateway-service.yml
The CORS configuration allows all headers and common HTTP methods from the localhost:4200 origin.
Service Routes
The gateway defines routes for each microservice using path-based predicates and load-balanced URIs.Authentication Service (Public)
Protected Services
All other services require authentication via theAuthFilter:
The
lb:// prefix in the URI enables load balancing through Eureka service discovery.Complete Route Table
| Service | Path Patterns | Authentication Required |
|---|---|---|
| Auth | /auth/**, /usuario/**, /rol/** | No |
| Catalogo | /categoria/**, /producto/**, /imagenes/** | Yes (except /imagenes/**) |
| Cliente | /cliente/** | Yes |
| Pagos | /pagos/** | Yes |
| Venta | /venta/** | Yes |
| Pedido | /pedido/** | Yes |
| Compra | /compra/** | Yes |
| Proveedor | /proveedor/** | Yes |
| Inventario | /inventario/** | Yes |
Authentication Filter
TheAuthFilter implements JWT token validation for protected routes.
Implementation
AuthFilter.java
Authentication Flow
- Path Exemption: Images at
/imagenes/**bypass authentication - Header Validation: Checks for
Authorizationheader presence - Token Format: Validates
Bearer <token>format - Token Validation: Calls auth service at
/auth/validateendpoint - Response: Returns 400 BAD_REQUEST for invalid tokens
WebClient Configuration
The gateway uses a load-balanced WebClient for inter-service communication:WebClientConfig.java
@LoadBalanced annotation enables Eureka-based service discovery for the WebClient.
Token DTO
TokenDto.java
Maven Dependencies
pom.xml
Running the Gateway
Prerequisites
- Config Server running on port 7070
- Eureka Server running on port 8090
- Auth Service registered with Eureka
Startup
- Connect to Config Server to fetch configuration
- Register with Eureka service registry
- Start accepting requests on port 8085
Testing Routes
Public Route (No Authentication)
Protected Route (With Authentication)
Best Practices
Security Considerations
Security Considerations
- Always use HTTPS in production
- Implement rate limiting for public endpoints
- Use secure credentials for Config Server
- Rotate JWT signing keys regularly
Performance Optimization
Performance Optimization
- Enable response caching where appropriate
- Configure connection pool sizes for WebClient
- Monitor gateway metrics and latency
- Use circuit breakers for resilience
Monitoring
Monitoring
- Enable actuator endpoints for health checks
- Track request/response metrics
- Log authentication failures
- Monitor service discovery events
Troubleshooting
| Issue | Solution |
|---|---|
| 503 Service Unavailable | Check if target service is registered in Eureka |
| 401 Unauthorized | Verify JWT token is valid and not expired |
| CORS errors | Ensure frontend origin is in allowedOrigins |
| Connection refused | Verify Config Server and Eureka are running |
Related Documentation
- Service Registry - Eureka service discovery
- Config Server - Centralized configuration
- Auth Service - Authentication and JWT generation