1. 程式人生 > >嵌入式Tomcat容器的參數(maxParameterCount)設定

嵌入式Tomcat容器的參數(maxParameterCount)設定

red common ret bstr class tex pub 設置 系統

背景

昨天同事遇到了error一起看了一下感覺比較重要在這記錄一下

基本情況是頁面上選中9K+的數據向後臺發送請求,然後系統就崩了。。。

error信息如下

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容器的工廠類

繼續看看這個類裏都什麽可用(源碼有點多就不貼了貼一張截圖)

技術分享圖片

這裏就是我們這次用到的函數了為什麽標記兩個因為上邊那個也可用,下面是官網給的說明

技術分享圖片

當然其它的屬性也可以在這裏設定貼一下官網連接就不再代碼體現了。

嵌入式Tomcat容器的參數(maxParameterCount)設定