RequestSize 过滤器
RequestSize Filter
当请求大小超过允许的限制时,RequestSize 过滤器可以阻止请求到达下游服务。该过滤器接受一个 maxSize 参数。maxSize 是 DataSize 类型,因此值可以定义为数字后跟可选的 DataUnit 后缀,例如 'KB' 或 'MB'。默认值为 'B',表示字节。这是以字节为单位定义的允许请求大小限制。以下配置示例展示了如何配置 RequestSize 过滤器:
spring:
cloud:
gateway:
server:
webmvc:
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.uri;
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())
.before(uri("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。