首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Spring Web Services中限制最大请求大小?

如何在Spring Web Services中限制最大请求大小?
EN

Stack Overflow用户
提问于 2020-07-02 15:56:28
回答 3查看 642关注 0票数 0

我有一个带有Spring Web Services的Spring Boot2应用程序,我尝试限制传入请求的最大大小。我尝试使用以下属性,但它不起作用。

代码语言:javascript
复制
server.tomcat.max-http-form-post-size
spring.servlet.multipart.max-request-size
spring.servlet.multipart.max-file-size

你知道在Spring Boot2应用程序中使用Spring Web Services限制传入请求的最大大小的可行解决方案吗?在Spring Web Services documentation中,有关于Spring Web Services提供基于SunJRE1.6HTTP服务器的传输的信息。也许这是个问题?

EN

回答 3

Stack Overflow用户

发布于 2020-07-02 16:02:24

将此Bean添加到您的应用程序中并设置所需的大小(当前为10 MB)

代码语言:javascript
复制
// Set maxPostSize of embedded tomcat server to 10 MB (default is 2 MB, not large enough to support file uploads > 1.5 MB)
    @Bean
    EmbeddedServletContainerCustomizer containerCustomizer() throws Exception {
        return (ConfigurableEmbeddedServletContainer container) -> {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
                tomcat.addConnectorCustomizers(
                    (connector) -> {
                        connector.setMaxPostSize(10000000); // 10 MB
                    }
                );
            }
        };
    }
票数 0
EN

Stack Overflow用户

发布于 2020-07-04 00:39:23

最后,我决定创建一个过滤器。

代码语言:javascript
复制
@Bean
public Filter maxRequestSizeFilter() {
    return new Filter() {
        @Override
        public void init(final FilterConfig filterConfig) {

        }

        @Override
        public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
            final FilterChain filterChain) throws IOException, ServletException {
            final long size = servletRequest.getContentLengthLong();
            if (size > maxRequestSizeInBytes) {
                ((HttpServletResponse) servletResponse).sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
            } else {
                    filterChain.doFilter(servletRequest, servletResponse);
            }
        }
        ...
    }
    ...
}
票数 0
EN

Stack Overflow用户

发布于 2021-03-22 17:36:08

您正在使用的是HTTP POST请求吗?

因为

  • spring.servlet.multipart.max-request-size
  • spring.servlet.multipart.max-file-size

完美工作

对于大型请求,GET不是最好的主意。如果你真的需要,你可以试试server.max-http-header-size

在tomcat 8.5上验证了所有3个参数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62691622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档