Skip to main content

API Gateway Security πŸ”

The API Gateway is the single entry point to the ShopVerse platform and acts as the central enforcement layer for security, routing, and access control.

All external client requests must pass through the API Gateway.


🎯 Responsibilities of the API Gateway​

The API Gateway is responsible for:

  • JWT validation
  • Authentication enforcement
  • Role extraction
  • Request routing
  • Header enrichment
  • Blocking unauthorized access
  • Centralized security policies

🧠 Why an API Gateway?​

Using an API Gateway provides:

  • Centralized security logic
  • No duplication of auth logic across services
  • Easier security updates
  • Reduced attack surface
  • Clean separation of concerns

It allows downstream services to focus only on business logic.


πŸ—οΈ High-Level Architecture​


πŸ”‘ JWT Validation Flow​


πŸ” Request Processing Steps​

  1. Client sends request with JWT

  2. Gateway validates token signature & expiration

  3. Gateway extracts user identity and role

  4. Gateway removes Authorization header

  5. Gateway injects trusted headers:

    • X-User-Email
    • X-User-Role
  6. Request is routed to target service


πŸ” Header-Based Trust Model​

Downstream services:

  • Do not accept JWT tokens
  • Trust only gateway-injected headers
  • Reject requests missing required headers

This prevents:

  • Token replay
  • Direct service access
  • Security bypass attempts

πŸ›‘οΈ Role-Based Access Control (RBAC)​

RBAC enforcement is shared:

  • Gateway β†’ coarse-grained access (public vs protected routes)
  • Services β†’ fine-grained access (role-based permissions)

Example:

  • Public β†’ GET /api/products
  • Protected β†’ POST /api/products (ADMIN)

🌐 Route Configuration Strategy​

The Gateway defines:

  • Public routes (no JWT required)
  • Protected routes (JWT required)
  • Admin-only routes (role enforced)

Example Public Routes:​

/api/auth/**
/api/products (GET)
/api/categories (GET)

πŸ”„ Failure Handling​

ScenarioResponse
Missing JWT401 Unauthorized
Invalid JWT401 Unauthorized
Insufficient Role403 Forbidden
Gateway DownSystem inaccessible

βš™οΈ Implementation Details (ShopVerse)​

  • Built using Spring Cloud Gateway
  • Reactive WebFlux-based filter
  • Custom JwtAuthFilter
  • Uses jjwt for token validation
  • Removes Authorization header post-validation
  • Injects identity headers

πŸ›‘οΈ Security Hardening Measures​

  • CSRF disabled (stateless API)
  • No form login or HTTP basic auth
  • CORS controlled centrally
  • Rate limiting (future-ready)
  • Logging for audit trails

πŸ“ˆ Scalability Considerations​

  • Stateless gateway
  • Horizontally scalable
  • Can be fronted by load balancer
  • Token validation is lightweight

🧠 Interview Explanation (One-Liner)​

β€œThe API Gateway acts as a centralized security layer where JWT is validated once and trusted identity headers are propagated to downstream services for RBAC.”


πŸ“Œ Summary​

The API Gateway in ShopVerse provides:

  • Centralized authentication
  • Consistent authorization
  • Secure request routing
  • Reduced service complexity

It is the security backbone of the entire system.