Tomcat的maxParameterCount&maxPostSize参数

Tomcat的maxParameterCount&maxPostSize参数

  • Tomcat的maxParameterCount&maxPostSize参数
    • 1.问题
      • 1.1问题现象
      • 1.2 参数总结
      • 1.3 问题总结
    • 2 Tomcat官网的解释
      • 2.1 到`https://tomcat.apache.org/`找到文档入口
      • 2.2 找到文档的`Reference`
      • 2.3 查看配置文件的参数
    • 3 文档看不明白,自己做实验吧。
      • 3.1 `maxParameterCount` 参数个数
      • 3.2 `maxPostSize`POST请求参数大小
    • 4.实验配置

Tomcat的maxParameterCount&maxPostSize参数

参考文章:
嵌入式Tomcat容器的参数(maxParameterCount&maxPostSize)设定,参数过多解决方案

1.问题

1.1问题现象

周五同事说,请求的参数拿不到了。但是同一个接口请求参数太大就没有参数了,参数少的话服务端是有参数的。

打开浏览器的控制台,发现POST的请求参数中的有一个参数很大,所有的参数加起来有2.8M了。网上查了一下Tomcat的配置,
原来配置文件中有一个masPostSize的参数。因此这个博客来看看tomcatmaxParameterCount&maxPostSize参数,看看是不是这个问题导致的。

这里截图看到线上是Content-Type: application/x-www-form-urlencoded;charset=UTF-8的POST请求类型,Content-Length:有问题的是2.8M,并不是这个截图所示的234B。
在这里插入图片描述

1.2 参数总结

  • maxParameterCount控制请求参数的个数,对于application/x-www-form-urlencoded or multipart/form-data的POST请求来说是请求参数和请求体参数总个数。超出的参数获取不到
  • maxPostSize控制POST请求参数大小的限制。
    • application/x-www-form-urlencoded大小超过的参数获取不到。
    • multipart/form-data 大小超过异常报错。

1.3 问题总结

tomcat的maxPostSize没有设置,默认的是2M,请求是application/x-www-form-urlencoded 类型的,所以也不会报错。参数字节数小的可以获取到,参数字节数大的就获取不到了。

2 Tomcat官网的解释

2.1 到https://tomcat.apache.org/找到文档入口

在这里插入图片描述

2.2 找到文档的Reference

在这里插入图片描述在这里插入图片描述

2.3 查看配置文件的参数

  • maxParameterCount
    • The maximum total number of request parameters (including uploaded files) obtained from the query string and, for POST requests, the request body if the content type is application/x-www-form-urlencoded or multipart/form-data. Request parameters 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 exceed the limit.
    • 参数个数,超出的部分会被忽略,默认是1w个参数
  • 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 MiB). Note that the FailedRequestFilter can be used to reject requests that exceed this limit.
    • POST请求体参数的大小,字节单位,这里没说超过了会怎么样。
      在这里插入图片描述
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!-- APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxPostSize="7"
               maxParameterCount="2"
               />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation. The default
         SSLImplementation will depend on the presence of the APR/native
         library and the useOpenSSL attribute of the AprLifecycleListener.
         Either JSSE or OpenSSL style configuration may be used regardless of
         the SSLImplementation selected. JSSE style configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true"
               maxParameterCount="1000"
               >
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the APR/native implementation which always uses
         OpenSSL for TLS.
         Either JSSE or OpenSSL style configuration may be used. OpenSSL style
         configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" SSLEnabled="true"
               maxParameterCount="1000"
               >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                         certificateFile="conf/localhost-rsa-cert.pem"
                         certificateChainFile="conf/localhost-rsa-chain.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <!--
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    -->

    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

3 文档看不明白,自己做实验吧。

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxPostSize="7"
               maxParameterCount="2"
               />
  • 参数个数最大2个
  • POST请求大小最大7个

3.1 maxParameterCount 参数个数

  • GET请求参数的个数超过之后,多出来的就取不到了。
? ~/data/ 
? ~/data/ curl -s  --location 'http://localhost:8080/?m=m&m1=m1&m2=m2' | jq .
{
  "m": [
    "m"
  ],
  "m1": [
    "m1"
  ]
}
? ~/data/ 
? ~/data/ curl -s  --location 'http://localhost:8080/?m=m&m1=m1' | jq .      
{
  "m": [
    "m"
  ],
  "m1": [
    "m1"
  ]
}
? ~/data/ 
  • POST的请求参数个数超过过之后,多出来的就取不到了。
? ~/data/ 
? ~/data/ curl -s  --location --request POST 'http://localhost:8080/test?m=m&m1=m1&m2=m2' | jq .
{
  "m": [
    "m"
  ],
  "m1": [
    "m1"
  ]
}
? ~/data/ 
? ~/data/ 
? ~/data/ curl -s  --location --request POST 'http://localhost:8080/test?m=m&m1=m1' | jq .      
{
  "m": [
    "m"
  ],
  "m1": [
    "m1"
  ]
}
? ~/data/ 
? ~/data/ curl --location 'http://localhost:8080/test?m=m&m1=m1&m2=m2' 
--header 'Content-Type: application/x-www-form-urlencoded' -s 
--data-urlencode 'm3=m3' | jq .
{
  "m": [
    "m"
  ],
  "m1": [
    "m1"
  ]
}
? ~/data/ 
? ~/data/ curl --location 'http://localhost:8080/test?m=m' 
--header 'Content-Type: application/x-www-form-urlencoded' 
--data-urlencode 'm3=m3' -s | jq .
{
  "m": [
    "m"
  ],
  "m3": [
    "m3"
  ]
}
? ~/data/ 
? ~/data/ 

3.2 maxPostSizePOST请求参数大小

  • Content-Type: application/x-www-form-urlencoded大小没有超过都可以获取到,超过大小都获取不到
? ~/data/ 
? ~/data/ curl --location 'http://localhost:8080/test' -s 
--header 'Content-Type: application/x-www-form-urlencoded' 
--data-urlencode 'j=12345' | jq .
{
  "j": [
    "12345"
  ]
}
? ~/data/ 
? ~/data/ 
? ~/data/ 
? ~/data/ curl --location 'http://localhost:8080/test' -s 
--header 'Content-Type: application/x-www-form-urlencoded' 
--data-urlencode 'j=123456' | jq .
{}
? ~/data/ 
? ~/data/ curl --location 'http://localhost:8080/test' -s 
--header 'Content-Type: application/x-www-form-urlencoded' 
--data-urlencode 'j=1' 
--data-urlencode 'i=2' | jq .
{
  "j": [
    "1"
  ],
  "i": [
    "2"
  ]
}
? ~/data/ 
? ~/data/ 
? ~/data/ curl --location 'http://localhost:8080/test' -s 
--header 'Content-Type: application/x-www-form-urlencoded' 
--data-urlencode 'j=1' 
--data-urlencode 'i=23' | jq .
{}
? ~/data/ 
? ~/data/ 
  • multipart/form-data; boundary=<calculated when request is sent> 大小没有超过都可以获取到,超过大小报错
? ~/data/ 
? ~/data/ curl --location 'http://localhost:8080/test' 
--form 'j="1234"' -s | jq . 
{
  "j": [
    "1234"
  ]
}
? ~/data/ curl --location 'http://localhost:8080/test' -s 
--form 'j="12345"'|jq .
{
  "timestamp": 1705816422926,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.web.multipart.MultipartException",
  "message": "Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector",
  "path": "/test"
}
? ~/data/ 

4.实验配置

? ~/data/  docker pull tomcat:8.5.98
? ~/data/  docker run -d -p 8080:8080 -v /Users/admin/data/tomcat/webapps:/usr/local/tomcat/webapps tomcat:8.5.98
90f2cfa859c67e3886f67d8b862005c196944cbc037efc64e2e1417b450ae174
? ~/data/ server.xml的配置见上文
? ~/data/ docker cp ./server.xml 90f2cfa859c6:/usr/local/tomcat/conf/server.xml
? ~/data/ java 代码:https://github.com/xiaolixi/spring/tree/main/springboot-resttemplate

https://tomcat.apache.org/tomcat-8.5-doc/servletapi/javax/servlet/ServletRequest.html#getParameterMap()
在这里插入图片描述