我有一个带有Spring Web Services的Spring Boot2应用程序,我尝试限制传入请求的最大大小。我尝试使用以下属性,但它不起作用。
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服务器的传输的信息。也许这是个问题?
发布于 2020-07-02 16:02:24
将此Bean添加到您的应用程序中并设置所需的大小(当前为10 MB)
// 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
}
);
}
};
}发布于 2020-07-04 00:39:23
最后,我决定创建一个过滤器。
@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);
}
}
...
}
...
}发布于 2021-03-22 17:36:08
您正在使用的是HTTP POST请求吗?
因为
完美工作
对于大型请求,GET不是最好的主意。如果你真的需要,你可以试试server.max-http-header-size
在tomcat 8.5上验证了所有3个参数
https://stackoverflow.com/questions/62691622
复制相似问题