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
的参数。因此这个博客来看看tomcat
的maxParameterCount&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请求体参数的大小,字节单位,这里没说超过了会怎么样。
Server port="8005" shutdown="SHUTDOWN">
Listener className="org.apache.catalina.startup.VersionLoggerListener" />
Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
GlobalNamingResources>
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>
Service name="Catalina">
Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxPostSize="7"
maxParameterCount="2"
/>
Engine name="Catalina" defaultHost="localhost">
Realm className="org.apache.catalina.realm.LockOutRealm">
Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
Realm>
Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %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 maxPostSize
POST请求参数大小
-
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=
大小没有超过都可以获取到,超过大小报错
~/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/lo服务器托管网cal/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()
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net