API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Lekcja

Wbudowane GatewayFilters, które warto znać

Poznaj najbardziej przydatne fabryki GatewayFilter dostępne od razu, aby rozwiązywać typowe zadania bez pisania własnego kodu.

Lekcja 4 z 413 kroki

Wbudowane GatewayFilters, które warto znać to bezpłatna lekcja API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Bezpłatny start

Ucz się API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Wbudowane GatewayFilters, które warto znać” jest bezpłatna?

Tak — pełny tekst „Wbudowane GatewayFilters, które warto znać” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), przejdź na CoddyKit PRO. Kurs API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera 4 lekcji w sumie.

Co nauczysz się w „Wbudowane GatewayFilters, które warto znać”?

Poznaj najbardziej przydatne fabryki GatewayFilter dostępne od razu, aby rozwiązywać typowe zadania bez pisania własnego kodu. Ćwiczysz API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Nie wymagamy żadnego doświadczenia. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Wbudowane GatewayFilters, które warto znać”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Tak. Każda lekcja API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Filtry globalne i GatewayFilterFactory
  2. Niestandardowe filtry żądań i odpowiedzi
  3. Przetwarzanie wstępne i końcowe za pomocą filtrów
  4. Wbudowane GatewayFilters, które warto znać
← Powrót do API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)