跳到主要内容
版本:5.0.1

网关处理器过滤器函数

DeepSeek V3 中英对照 Gateway Handler Filter Functions

前置过滤器函数​

WebMvc.fn API 中有一个“前置”过滤器函数的概念,它是一个 java.util.Function<ServerRequest, ServerRequest>,仅作用于请求。许多前置过滤器函数在 org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions 中作为静态工厂方法被引用。它们在 org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions 中被适配为通用的 org.springframework.web.servlet.function.HandlerFilterFunction<ServerResponse, ServerResponse>。两者都可以使用,但更具体的 BeforeFilterFunctions 是首选,因为它更明确。

After Filter Functions​

WebMvc.fn API 中有一个 "after" 过滤器函数的概念,它是一个 java.util.BiFunction<ServerRequest, ServerResponse, ServerResponse>,可以修改响应。许多 after 过滤器函数在 org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions 中作为静态工厂方法被引用。它们在 org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions 中被适配为通用的 org.springframework.web.servlet.function.HandlerFilterFunction<ServerResponse, ServerResponse>。两者都可以使用,但更具体的 AfterFilterFunctions 是首选,因为它更明确。

高级筛选函数​

有些过滤器无法简单地归类为前置或后置过滤器。这些过滤器在代理请求发起前后均会执行。其中包括位于 org.springframework.cloud.gateway.server.mvc.filter 包中的 BodyFilterFunctions、Bucket4jFilterFunctions、CircuitBreakerFilterFunctions、LoadBalancerFilterFunctions、RetryFilterFunctions 和 TokenRelayFilterFunctions。

章节总结​