RequestSize
过滤器
RequestSize
Filter
当请求大小超过允许的限制时,RequestSize
过滤器可以阻止请求到达下游服务。该过滤器接受一个 maxSize
参数。maxSize
是一个 DataSize
类型,因此值可以定义为一个数字,后面跟一个可选的 DataUnit
后缀,如 'KB' 或 'MB'。默认单位为 'B',表示字节。它是请求允许的大小限制,以字节为单位定义。以下代码清单配置了一个 RequestSize
过滤器:
spring:
cloud:
gateway:
mvc:
routes:
- id: request_size_route
uri: http://localhost:8080
predicates:
- Path=/upload
filters:
- name: RequestSize
args:
maxSize: 5000000
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.requestSize;
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> gatewayRouterFunctionsRequestSize() {
return route("request_size_route")
.GET("/upload", http("http://localhost:8080"))
.before(requestSize("5000000"))
.build();
}
}
RequestSize
过滤器在请求因大小被拒绝时,将响应状态设置为 413 Payload Too Large
,并添加一个 errorMessage
头。以下示例展示了这样的 errorMessage
:
errorMessage : Request size is larger than permissible limit. Request size is 6.0 MB where permissible limit is 5.0 MB
备注
如果在路由定义中没有作为过滤器参数提供,默认的请求大小设置为 5 MB。