RewriteLocationResponseHeader フィルター

RewriteLocationResponseHeader フィルターは、通常、バックエンド固有の詳細を削除するために、Location レスポンスヘッダーの値を変更します。stripVersionModelocationHeaderNamehostValueprotocolsRegex パラメーターを受け取ります。次のリストは、RewriteLocationResponseHeader フィルターを構成します。

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: rewritelocationresponseheader_route
          uri: http://example.org
          filters:
          - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.addResponseHeader;
import static org.springframework.cloud.gateway.server.mvc.filter.RewriteLocationResponseHeaderFilterFunctions.StripVersion;
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> gatewayRouterFunctionsRewriteLocationResponseHeader() {
        return route("rewritelocationresponseheader_route")
				.GET("/**", http("https://example.org"))
					.after(rewriteLocationResponseHeader(config -> config.setLocationHeaderName("Location").setStripVersion(StripVersion.AS_IN_REQUEST)))
					.build();
    }
}

例: POST api.example.com/some/object/name (英語) のリクエストの場合、object-service.prod.example.net/v2/some/object/id (英語)  の Location レスポンスヘッダー値は api.example.com/some/object/id (英語) として書き換えられます。

stripVersionMode パラメーターには、次の可能な値があります: NEVER_STRIPAS_IN_REQUEST (デフォルト)、ALWAYS_STRIP

  • NEVER_STRIP: 元のリクエストパスにバージョンが含まれていない場合でも、バージョンは削除されません。

  • AS_IN_REQUEST: バージョンは、元のリクエストパスにバージョンが含まれていない場合にのみ削除されます。

  • ALWAYS_STRIP: 元のリクエストパスにバージョンが含まれている場合でも、バージョンは常に削除されます。

hostValue パラメーターが指定されている場合は、レスポンス Location ヘッダーの host:port 部分を置き換えるために使用されます。指定されていない場合は、Host リクエストヘッダーの値が使用されます。

protocolsRegex パラメーターは、プロトコル名が一致する有効な正規表現 String である必要があります。一致しない場合、フィルターは何もしません。デフォルトは http|https|ftp|ftps です。