RemoveResponseHeader 過濾器

RemoveResponseHeader 過濾器接受一個 name 引數,該引數是需要刪除的響應頭名稱。以下列表展示瞭如何配置一個 RemoveResponseHeader 過濾器

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: removeresponseheader_route
          uri: https://example.org
          predicates:
          - Path=/anything/removeresponseheader
          filters:
          - RemoveResponseHeader=X-Response-Foo
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.removeResponseHeader;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsRemoveResponseHeader() {
        return route("removeresponseheader_route")
            .GET("/anything/removeresponseheader", http())
            .before(uri("https://example.org"))
            .after(removeResponseHeader("X-Response-Foo"))
            .build();
    }
}

這將移除響應中的 X-Response-Foo 響應頭,然後再將其返回給閘道器客戶端。

為了移除任何敏感響應頭,您應該為所有需要移除敏感頭的路由配置此過濾器。此外,您還可以透過使用 spring.cloud.gateway.default-filters 配置一次此過濾器,使其應用於所有路由。