RewriteLocationResponseHeader
Filter
RewriteLocationResponseHeader
过滤器修改 Location
响应标头的值,通常是为了去除后端特定的详细信息。它使用 stripVersionMode
、locationHeaderName
、hostValue
和 protocolsRegex
参数。以下列表配置了一个 RewriteLocationResponseHeader
过滤器:
The RewriteLocationResponseHeader
filter modifies the value of the Location
response header, usually to get rid of backend-specific details.
It takes the stripVersionMode
, locationHeaderName
, hostValue
, and protocolsRegex
parameters.
The following listing configures a RewriteLocationResponseHeader
filter:
spring:
cloud:
gateway:
mvc:
routes:
- id: rewritelocationresponseheader_route
uri: http://example.org
filters:
- RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
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 [role="bare"]https://api.example.com/some/object/name
,Location
响应头的值为 https://object-service.prod.example.net/v2/some/object/id
将被重写为 https://api.example.com/some/object/id
。
For example, for a request of POST [role="bare"]https://api.example.com/some/object/name
, the Location
response header value of https://object-service.prod.example.net/v2/some/object/id
is rewritten as https://api.example.com/some/object/id
.
stripVersionMode
参数有以下可能值:NEVER_STRIP
、AS_IN_REQUEST
(默认值)和 ALWAYS_STRIP
。
The stripVersionMode
parameter has the following possible values: NEVER_STRIP
, AS_IN_REQUEST
(default), and ALWAYS_STRIP
.
-
NEVER_STRIP
: The version is not stripped, even if the original request path contains no version. -
AS_IN_REQUEST
: The version is stripped only if the original request path contains no version. -
ALWAYS_STRIP
: The version is always stripped, even if the original request path contains version.
如果提供了 hostValue
参数,则将其用于替换响应 Location
标头的 host:port
部分。如果未提供,则使用 Host
请求标头。
The hostValue
parameter, if provided, is used to replace the host:port
portion of the response Location
header.
If it is not provided, the value of the Host
request header is used.
protocolsRegex
参数必须是一个有效的正则表达式 String
,协议名称将根据该正则表达式进行匹配。如果未匹配,过滤器将不执行任何操作。默认值为 http|https|ftp|ftps
。
The protocolsRegex
parameter must be a valid regex String
, against which the protocol name is matched.
If it is not matched, the filter does nothing.
The default is http|https|ftp|ftps
.