知っておきたい組み込みGatewayFilter
すぐに使える便利なGatewayFilterファクトリを確認し、カスタムコードを書かずに一般的な処理を解決できるようにします。
「知っておきたい組み込みGatewayFilter」はCoddyKit上の無料API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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, gatewayAddResponseHeader
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-gatewayRewritePath
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 -> /usersPrefixPath
The reverse of stripping, PrefixPath prepends a segment to the path sent upstream.
filters:
- PrefixPath=/internal
# /users -> /internal/usersSetStatus
SetStatus forces the HTTP status code of the response, handy for maintenance routes or deprecation notices.
filters:
- SetStatus=410RedirectTo
RedirectTo returns a redirect response with a status and target URL instead of proxying.
filters:
- RedirectTo=302, https://new.example.comRemoveRequestHeader
Strip a header before it reaches the backend with RemoveRequestHeader, for example to drop a client-supplied header you do not trust.
filters:
- RemoveRequestHeader=CookieA 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, edgeDefault 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, trueQuick 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-filtersapply globally
Reach for these before writing custom code, then build custom filters only when nothing fits.
よくある質問
「知っておきたい組み込みGatewayFilter」レッスンは無料ですか?
はい。「知っておきたい組み込みGatewayFilter」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)コースには全4レッスンが含まれています。
「知っておきたい組み込みGatewayFilter」で何を学びますか?
すぐに使える便利なGatewayFilterファクトリを確認し、カスタムコードを書かずに一般的な処理を解決できるようにします。 ブラウザで直接実行するハンズオンコードでAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)を始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「知っておきたい組み込みGatewayFilter」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンでコードを書いて実行できますか?
はい。すべてのAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- グローバルフィルターとGatewayFilterFactory
- カスタムリクエスト/レスポンスフィルター
- フィルターによる前処理と後処理
- 知っておきたい組み込みGatewayFilter