SetRequestHostHeader Filter

在某些情况下,可能需要覆盖主机标头。在这种情况下,SetRequestHostHeader 过滤器可以用指定值替换现有主机标头。此过滤器采用 host 参数。以下清单配置了一个 SetRequestHostHeader 过滤器:

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: set_request_host_header_route
          uri: http://localhost:8080
          predicates:
          - Path=/headers
          filters:
          - name: SetRequestHostHeader
            args:
              host: example.org
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.setRequestHostHeader;
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> gatewayRouterFunctionsSetRequestHostHeader() {
		return route("set_request_host_header_route")
				.GET("/headers", http("http://localhost:8080"))
					.before(setRequestHostHeader("example.org"))
					.build();
    }
}

SetRequestHostHeader 过滤器将主机标头的值替换为 example.org