springboot maxParameterCount [ More than the maximum number of request parameters (GET plus POST)]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zzuchenyb/article/details/89326654

More than the maximum number of request parameters (GET plus POST) for a single request ([10,000]) were detected.

Any parameters beyond this limit have been ignored. To change this limit, set the maxParameterCount attribute on the Connector.

简说 单次请求的参数超出限制,通过maxParameterCount来更改容器的限制。

经验里对于tomcat容器的设定最对就是端口号,超时,最大线程的设置比较多

这个【maxParameterCount 】的设定还没有过然后到网上去翻了翻,

在官网的文档(tomcat doc)里找到了如下

maxParameterCount The maximum number of parameter and value pairs (GET plus POST) which will be automatically parsed by the container. Parameter and value pairs beyond this limit will be ignored. A value of less than 0 means no limit. If not specified, a default of 10000 is used. Note that FailedRequestFilter filter can be used to reject requests that hit the limit.
maxPostSize The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than zero. If not specified, this attribute is set to 2097152 (2 megabytes). Note that the FailedRequestFilter can be used to reject requests that exceed this limit.

简说

maxParameterCount 是tomcat容器来限定你 单次请求的参数的最大数量,默认是10000。所以通过这个属性你可以根据情况给设定适当的值。当然也有超出1w的情况怎么办?

上面文档里也有给出答案 小于0的设定可以禁用此制限。这也是很多网上资料设置-1的原因。

maxPostSize 是http-post单次请求内容或者说数据的最大限制,默认值为2M。同样小于0的设定可以禁用此制限

具体使用

tomcat/conf/server.xml文件中找到如下节点

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

(这里有好几个connector看自己用的是哪个端口的)

修改后

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxPostSize="-1" maxParameterCount="-1"/>

这是通常的做法,但有时如果tomcat容器内置的话你可能都找不到server.xml文件,

比如spring boo项目内置tomcat

这时候通常会想到 可以配置在application.properties里

然后翻一下看看application.properties里怎么配置

spring-boot的doc看到只看到如下

server.tomcat.max-http-post-size=0 # Maximum size in bytes of the HTTP post content.

并没有找到关于maxParameterCount信息,猜测不支持在配置文件里配置

继续翻

找到可以写在java类里的方法

    @Bean
    public EmbeddedServletContainerFactory mbeddedServletContainerFactory() {
        TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory = new TomcatEmbeddedServletContainerFactory();
        
        tomcatEmbeddedServletContainerFactory.addConnectorCustomizers(connector ->{
            connector.setMaxPostSize(2);
            System.out.println("connector.getMaxPostSize: "+connector.getMaxPostSize());
            System.out.println("connector.getPort: "+connector.getPort());
        });
        
        return tomcatEmbeddedServletContainerFactory;
    }

 代码本身并不多,过程比较曲折,大概说一下

EmbeddedServletContainerCustomizer

这是一个自定义内置容器的接口,通过实现它可以创建内置容器

然后还找到已经实现了它类,

AbstractEmbeddedServletContainerFactory, 
JettyEmbeddedServletContainerFactory, 
TomcatEmbeddedServletContainerFactory, 
UndertowEmbeddedServletContainerFactory

这里在启动类中定制tomcat容器的工厂类并定制对应的connector参数。

猜你喜欢

转载自blog.csdn.net/zzuchenyb/article/details/89326654