erd*_*run 1 rest post json tomcat spring-boot
我正在使用Spring boot嵌入式tomcat来发布休息服务.
Spring引导版使用了最新的"1.3.6.RELEASE"
我要求将application/json post请求大小限制为10KB.
厌倦了这个解决方案但没有工作, 在Spring Boot中增加HTTP Post maxPostSize
请帮忙.
谢谢,阿伦.
配置最大尺寸后仅适用于有要求Content-Type的multipart/form-data或application/x-www-form-urlencoded.Tomcat中没有开箱即用的机制来限制具有任何其他内容类型的请求的请求主体的大小,因此您必须编写一些代码.
您可以使用过滤器限制内容长度.例如,将以下类添加到Spring Boot应用程序将拒绝具有与10000 Content-Type兼容application/json且Content-Length超过10000 的请求:
@Component
static class ApplicationJsonRequestSizeLimitFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (isApplicationJson(request) && request.getContentLengthLong() > 10000) {
throw new IOException("Request content exceeded limit of 10000 bytes");
}
filterChain.doFilter(request, response);
}
private boolean isApplicationJson(HttpServletRequest httpRequest) {
return (MediaType.APPLICATION_JSON.isCompatibleWith(MediaType
.parseMediaType(httpRequest.getHeader(HttpHeaders.CONTENT_TYPE))));
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,此过滤器不会拒绝没有Content-Length超过10000字节的标头的请求,例如使用分块编码的请求.
| 归档时间: |
|
| 查看次数: |
2658 次 |
| 最近记录: |