RequestSize
Filter
当请求大小大于允许的限制时,RequestSize
过滤器可以限制请求到达下游服务。此过滤器采用一个 maxSize
参数。maxSize
是 DataSize
类型,因此值可以定义为一个数字,后跟一个可选的 DataUnit
后缀,如 'KB' 或 'MB'。默认值为 'B'(字节)。它是以字节为单位定义的请求允许大小限制。以下列表配置了 RequestSize
过滤器:
When the request size is greater than the permissible limit, the RequestSize
filter can restrict a request from reaching the downstream service.
The filter takes a maxSize
parameter.
The maxSize
is a DataSize
type, so values can be defined as a number followed by an optional DataUnit
suffix such as 'KB' or 'MB'. The default is 'B' for bytes.
It is the permissible size limit of the request defined in bytes.
The following listing configures a RequestSize
filter:
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
:
The RequestSize
filter sets the response status as 413 Payload Too Large
with an additional header errorMessage
when the request is rejected due to size. The following example shows such an errorMessage
:
errorMessage : Request size is larger than permissible limit. Request size is 6.0 MB where permissible limit is 5.0 MB
如果没有在路由定义的 filter 参数中提供默认请求大小,则将其设置为 5 MB。 |
The default request size is set to five MB if not provided as a filter argument in the route definition. |