您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

spring启动嵌入式Tomcat“ application / json”发布请求限制为10KB

spring启动嵌入式Tomcat“ application / json”发布请求限制为10KB

配置最大尺寸后仅适用于有要求Content-Typemultipart/form-dataapplication/x-www-form- urlencoded。Tomcat中没有开箱即用的机制来限制具有任何其他内容类型的请求的请求主体的大小,因此您必须编写一些代码

您可以使用过滤器限制内容长度。例如,将以下类添加到您的Spring Boot应用程序将拒绝与Content- Type兼容application/jsonContent-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))));
    }

}

请注意,如果Content-Length标头超过10000个字节(例如,使用分块编码的标头),则此过滤器不会拒绝请求。

Java 2022/1/1 18:14:47 有671人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶