0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · 课时

您应了解的内置 GatewayFilters

探索最实用的开箱即用 GatewayFilter 工厂,无需编写自定义代码即可解决常见任务。

您应了解的内置 GatewayFilters 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Filters You Get for Free

Before writing custom filters, know that Spring Cloud Gateway ships with dozens of ready-made GatewayFilter factories. You attach them per route in configuration.

This lesson tours the ones you will reach for most often.

AddRequestHeader

AddRequestHeader injects a header into every request before it reaches the backend, useful for tagging traffic from the gateway.

filters:
  - AddRequestHeader=X-Source, gateway

AddResponseHeader

Likewise, AddResponseHeader adds a header to the response sent back to the client, for example a marker showing which gateway handled it.

filters:
  - AddResponseHeader=X-Handled-By, edge-gateway

RewritePath

RewritePath uses a regex to transform the incoming path before forwarding. Strip a public prefix while keeping the rest.

filters:
  - RewritePath=/api/(?<seg>.*), /$\{seg}

StripPrefix

When you just need to drop leading path segments, StripPrefix is simpler than a rewrite. StripPrefix=1 removes the first segment.

filters:
  - StripPrefix=1
# /service/users -> /users

PrefixPath

The reverse of stripping, PrefixPath prepends a segment to the path sent upstream.

filters:
  - PrefixPath=/internal
# /users -> /internal/users

SetStatus

SetStatus forces the HTTP status code of the response, handy for maintenance routes or deprecation notices.

filters:
  - SetStatus=410

RedirectTo

RedirectTo returns a redirect response with a status and target URL instead of proxying.

filters:
  - RedirectTo=302, https://new.example.com

RemoveRequestHeader

Strip a header before it reaches the backend with RemoveRequestHeader, for example to drop a client-supplied header you do not trust.

filters:
  - RemoveRequestHeader=Cookie

A Full Route Example

Filters stack in declared order. Here a route strips a prefix, tags the request, and marks the response.

routes:
  - id: users
    uri: http://users-svc:8080
    predicates:
      - Path=/api/users/**
    filters:
      - StripPrefix=2
      - AddRequestHeader=X-Source, gateway
      - AddResponseHeader=X-Handled-By, edge

Default Filters

Use default-filters to apply a filter to every route at once, such as adding a correlation header everywhere.

spring:
  cloud:
    gateway:
      default-filters:
        - AddResponseHeader=X-Gateway, true

Quick Check

You want to turn /api/users into /users by dropping the first path segment. Which filter is the simplest fit?

Recap

You now have a toolbox of built-in filters:

  • Header filters: Add/Remove request and response headers
  • Path filters: StripPrefix, PrefixPath, RewritePath
  • Response control: SetStatus, RedirectTo
  • default-filters apply globally

Reach for these before writing custom code, then build custom filters only when nothing fits.

常见问题解答

「您应了解的内置 GatewayFilters」课时是免费的吗?

是的 — 「您应了解的内置 GatewayFilters」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

「您应了解的内置 GatewayFilters」这节课中我会学到什么?

探索最实用的开箱即用 GatewayFilter 工厂,无需编写自定义代码即可解决常见任务。 你通过在浏览器中直接运行的动手代码来练习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「您应了解的内置 GatewayFilters」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课中编写并运行代码吗?

能。每节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 全局过滤器与 GatewayFilterFactory
  2. 自定义请求与响应过滤器
  3. 使用过滤器进行前置与后置处理
  4. 您应了解的内置 GatewayFilters
← 返回 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)