A GatewayFilter in Spring Cloud Gateway is a component that runs on a single route and can inspect or change the request before it is proxied to the downstream service, and the response on the way back. Filters are how a gateway adds headers, rewrites paths, authenticates callers, applies rate limits, retries and circuit breakers without touching the backend services.
Filters that should apply to every route are written as a GlobalFilter instead, which has the same method signature but no per-route configuration.
A request reaches the gateway, the route predicates decide which route matches, and then the filter chain for that route runs. The chain has three parts: global filters, the filters declared on the route, and the built-in routing filter that actually calls the downstream service. They are sorted by order, and each filter decides whether to continue the chain.
Spring Cloud Gateway is built on Spring WebFlux, so filters are non-blocking and return Mono<Void>. Any blocking call inside a filter stalls an event-loop thread, so avoid it.
Both interfaces have one method:
You do work before calling chain.filter(exchange) (the pre phase), and attach work with then() or doFinally after it (the post phase).
| Type | Applies to | Configured how |
|---|---|---|
GatewayFilter | Only routes that list it | In route definitions, usually through a filter factory |
GlobalFilter | Every route | Registered as a Spring bean |
A common mistake in older tutorials is to say that global and route filters both implement GatewayFilter. They do not: GlobalFilter is a separate interface.
Spring Cloud Gateway ships many filter factories, referenced by name without the GatewayFilterFactory suffix. Examples include AddRequestHeader, StripPrefix, RewritePath, Retry, RequestRateLimiter and CircuitBreaker. In YAML:
Newer Spring Cloud release trains moved the WebFlux gateway properties under spring.cloud.gateway.server.webflux, so check the property prefix for your version. Spring Cloud Gateway also has a Server MVC variant for servlet applications.
To create a configurable filter that you can use by name, extend AbstractGatewayFilterFactory. The class name must end with GatewayFilterFactory.
Use it in a route with - RequestTiming and optional arguments.
A global filter is a bean that implements GlobalFilter and, usually, Ordered:
For real authentication, prefer Spring Security's WebFlux support (an OAuth2 resource server) and keep filters for gateway-specific concerns. Lower order values run earlier in the pre phase and later in the post phase.
WebTestClient.A GatewayFilter is attached to specific routes. A GlobalFilter runs for every route. Both use the same filter(exchange, chain) signature.
Chain .then(Mono.fromRunnable(...)) after chain.filter(exchange). This is the post phase.
Implement Ordered or annotate with @Order. For route filters, declaration order in the route also matters.
Yes. Write a response, for example a 401, and return exchange.getResponse().setComplete() without calling the chain.
GatewayFilter runs per route and GlobalFilter runs for all routes, and both wrap the downstream call in a reactive chain with a pre and a post phase. Use built-in factories for headers, paths, retries and rate limits, write custom factories for reusable route logic, and keep every filter non-blocking.