PreserveHostHeader 篩檢程式

PreserveHostHeader 篩檢程式沒有參數。此篩檢程式設定一個請求屬性,HandlerFunction 會檢查該屬性,以判斷是否應傳送原始主機標頭,而不是 HTTP 用戶端所決定的主機標頭。以下範例設定 PreserveHostHeader 篩檢程式

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: preserve_host_route
          uri: https://example.org
          filters:
          - PreserveHostHeader
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.preserveHostHeader;
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> gatewayRouterFunctionsPreserveHostHeader() {
		return route("preserve_host_route")
				.GET("/**", http("https://example.org"))
					.before(preserveHostHeader())
					.build();
    }
}