GATEWAY

This service acts as the API Gateway and sole entry point in a microservice ecosystem. Built on the reactive Spring Cloud Gateway (WebFlux) framework, it provides high-throughput, non-blocking edge routing, load balancing, multi-tenant context enrichment, and frontline security enforcement.

Technology Stack & Compatibility

Key Features

Security & Token Validation

As the edge proxy, this application is configured as an OAuth2 Resource Server validating stateless JSON Web Tokens (JWTs).

Custom Gateway Filter Chain

Routing logic goes beyond basic path forwarding. Employed a custom MyGatewayFilter (implemented via GatewayFilter) applied explicitly inside a RouteLocator bean.

The gateway filter sequence enforces the following pipeline:

Configuration and Code Implementation

Identity Provider (IdP) Setup

Configure the application properties to locate the JWKS endpoint used to decode and verify signatures:

spring: security: oauth2: resourceserver: jwt: jwk-set-uri: https://auth-server.com/auth/.well-known/jwks.json

CORS & CSRF Mitigation

Because this is a web-facing boundary, strict cross-origin rules apply:

Security Configuration

The gateway maintains a stateless SecurityWebFilterChain incorporating a global CorsWebFilter bean and cookie-backed CSRF mitigation optimized for Single Page Applications (SPAs).

Because standard Spring Security CSRF tokens are deferred (lazy) by default in reactive environments, a custom WebFilter is placed in the pipeline to explicitly subscribe to and flush the token cookie (CookieServerCsrfTokenRepository) on the client's initial metadata GET request.

// Conceptualized CSRF & Filter configuration within Configuration file .csrf(csrf -> csrf .csrfTokenRepository(cookieServerCsrfTokenRepository()) // Bean method name .csrfTokenRequestHandler(new ServerCsrfTokenRequestAttributeHandler()) )

Filter Ordering: The custom CSRF activation filter is configured to execute immediately after SecurityWebFiltersOrder.CSRF to ensure it successfully forces token generation.

Dynamic Route Routing

Routes are explicitly managed as Java beans rather than raw configuration files to cleanly embed the custom filter logic.

@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder, MyGatewayFilter gatewayFilter) { return builder.routes() .route("portfolio-service", r -> r.path("/portfolio/**") .filters(f -> f.filter(gatewayFilter)) .uri("https://my-portfolio.com") ) .build(); }

Project Structure

├───config/ #SecurityWebFilterChain, CorsWebFilter, CSRF and RouteLocator beans configured ├───filters/ #CSRF filter to trigger a token, Gatewat filter to validate authentication, authorization, tenant-info └───properties/ #Java 17 configuration records

Decision Log (Architecture Decision Records)

ADR 1: WebFlux over Spring MVC

ADR 2: Java Route Definitions over YAML

ADR 3: Edge-Enforced Multi-Tenancy Guardrails

Troubleshooting

Tracking Routing Decisions

If traffic is returning 404 Not Found or hitting the incorrect microservice, elevate the logging thresholds in local configuration profile to expose the gateway's internal evaluation pipeline:

logging: level: org.springframework.cloud.gateway: TRACE org.springframework.security: DEBUG

Missing CSRF Cookie on Initial Load

If front-end applications fail because of a missing XSRF-TOKEN cookie, verify that the client application sends an initial metadata GET request. The custom CsrfFilter will not flush the cookie to the client until a subscription is explicitly triggered on that initial connection loop.

Future Enhancements