最新の安定バージョンについては、Spring Cloud Gateway 5.0.1 を使用してください! |
SetStatus フィルター
SetStatus フィルターは、単一パラメーター status を取ります。有効な Spring HttpStatus である必要があります。整数値 404 または列挙の文字列表現 NOT_FOUND の場合があります。次のリストは、SetStatus フィルターを構成します。
application.yml
spring:
cloud:
gateway:
mvc:
routes:
- id: setstatusstring_route
uri: https://example.org
predicates:
- Path=/path1
filters:
- SetStatus=UNAUTHORIZED
- id: setstatusint_route
uri: https://example.org
predicates:
- Path=/path2
filters:
- SetStatus=401GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.setStatus;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
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> gatewayRouterFunctionsSetStatus() {
return route("setstatus_route")
.GET("/path1", http())
.before(uri("https://example.org"))
// setStatus("UNAUTHORIZED") works as well
.after(setStatus(HttpStatus.UNAUTHORIZED))
.build().and(route("setstatusint_route")
.before(uri("https://example.org"))
.GET("/path2", http())
.after(setStatus("401"))
.build());
}
}いずれの場合も、レスポンスの HTTP ステータスは 401 に設定されます。