Skip to content

Commit 3a6c2b1

Browse files
committed
Add configuration property to customize the Tomcat connector's max parameter
Closes GH-43275
1 parent d8c41c2 commit 3a6c2b1

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* @author Florian Storz
7474
* @author Michael Weidmann
7575
* @author Lasse Wulff
76+
* @author Yanming Zhou
7677
* @since 1.0.0
7778
*/
7879
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
@@ -513,13 +514,11 @@ public static class Tomcat {
513514
*/
514515
private DataSize maxHttpResponseHeaderSize = DataSize.ofKilobytes(8);
515516

516-
public DataSize getMaxHttpFormPostSize() {
517-
return this.maxHttpFormPostSize;
518-
}
519-
520-
public void setMaxHttpFormPostSize(DataSize maxHttpFormPostSize) {
521-
this.maxHttpFormPostSize = maxHttpFormPostSize;
522-
}
517+
/**
518+
* Maximum number of parameters (GET plus POST) that will be automatically parsed
519+
* by the container. A value of less than 0 means no limit.
520+
*/
521+
private int maxParameterCount = 10000;
523522

524523
public Accesslog getAccesslog() {
525524
return this.accesslog;
@@ -669,6 +668,22 @@ public void setMaxHttpResponseHeaderSize(DataSize maxHttpResponseHeaderSize) {
669668
this.maxHttpResponseHeaderSize = maxHttpResponseHeaderSize;
670669
}
671670

671+
public DataSize getMaxHttpFormPostSize() {
672+
return this.maxHttpFormPostSize;
673+
}
674+
675+
public void setMaxHttpFormPostSize(DataSize maxHttpFormPostSize) {
676+
this.maxHttpFormPostSize = maxHttpFormPostSize;
677+
}
678+
679+
public int getMaxParameterCount() {
680+
return this.maxParameterCount;
681+
}
682+
683+
public void setMaxParameterCount(int maxParameterCount) {
684+
this.maxParameterCount = maxParameterCount;
685+
}
686+
672687
/**
673688
* Tomcat access log properties.
674689
*/

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* @author Parviz Rozikov
6363
* @author Florian Storz
6464
* @author Michael Weidmann
65+
* @author Yanming Zhou
6566
* @since 2.0.0
6667
*/
6768
public class TomcatWebServerFactoryCustomizer
@@ -119,6 +120,8 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
119120
.asInt(DataSize::toBytes)
120121
.when((maxHttpFormPostSize) -> maxHttpFormPostSize != 0)
121122
.to((maxHttpFormPostSize) -> customizeMaxHttpFormPostSize(factory, maxHttpFormPostSize));
123+
map.from(properties::getMaxParameterCount)
124+
.to((maxParameterCount) -> customizeMaxParameterCount(factory, maxParameterCount));
122125
map.from(properties::getAccesslog)
123126
.when(ServerProperties.Tomcat.Accesslog::isEnabled)
124127
.to((enabled) -> customizeAccessLog(factory));
@@ -292,6 +295,10 @@ private void customizeMaxHttpFormPostSize(ConfigurableTomcatWebServerFactory fac
292295
factory.addConnectorCustomizers((connector) -> connector.setMaxPostSize(maxHttpFormPostSize));
293296
}
294297

298+
private void customizeMaxParameterCount(ConfigurableTomcatWebServerFactory factory, int maxParameterCount) {
299+
factory.addConnectorCustomizers((connector) -> connector.setMaxParameterCount(maxParameterCount));
300+
}
301+
295302
private void customizeAccessLog(ConfigurableTomcatWebServerFactory factory) {
296303
ServerProperties.Tomcat tomcatProperties = this.serverProperties.getTomcat();
297304
AccessLogValve valve = new AccessLogValve();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* @author Chris Bono
7070
* @author Parviz Rozikov
7171
* @author Lasse Wulff
72+
* @author Yanming Zhou
7273
*/
7374
@DirtiesUrlFactories
7475
class ServerPropertiesTests {
@@ -199,7 +200,7 @@ void testCustomizedMimeMapping() {
199200
}
200201

201202
@Test
202-
void testCustomizeUriEncoding() {
203+
void testCustomizeTomcatUriEncoding() {
203204
bind("server.tomcat.uri-encoding", "US-ASCII");
204205
assertThat(this.properties.getTomcat().getUriEncoding()).isEqualTo(StandardCharsets.US_ASCII);
205206
}
@@ -235,17 +236,23 @@ void testCustomizeTomcatKeepAliveTimeoutWithInfinite() {
235236
}
236237

237238
@Test
238-
void customizeMaxKeepAliveRequests() {
239+
void testCustomizeTomcatMaxKeepAliveRequests() {
239240
bind("server.tomcat.max-keep-alive-requests", "200");
240241
assertThat(this.properties.getTomcat().getMaxKeepAliveRequests()).isEqualTo(200);
241242
}
242243

243244
@Test
244-
void customizeMaxKeepAliveRequestsWithInfinite() {
245+
void testCustomizeTomcatMaxKeepAliveRequestsWithInfinite() {
245246
bind("server.tomcat.max-keep-alive-requests", "-1");
246247
assertThat(this.properties.getTomcat().getMaxKeepAliveRequests()).isEqualTo(-1);
247248
}
248249

250+
@Test
251+
void testCustomizeTomcatMaxParameterCount() {
252+
bind("server.tomcat.max-parameter-count", "100");
253+
assertThat(this.properties.getTomcat().getMaxParameterCount()).isEqualTo(100);
254+
}
255+
249256
@Test
250257
void testCustomizeTomcatMinSpareThreads() {
251258
bind("server.tomcat.threads.min-spare", "10");
@@ -379,6 +386,12 @@ void tomcatMaxHttpPostSizeMatchesConnectorDefault() {
379386
.isEqualTo(getDefaultConnector().getMaxPostSize());
380387
}
381388

389+
@Test
390+
void tomcatMaxParameterCountMatchesConnectorDefault() {
391+
assertThat(this.properties.getTomcat().getMaxParameterCount())
392+
.isEqualTo(getDefaultConnector().getMaxParameterCount());
393+
}
394+
382395
@Test
383396
void tomcatBackgroundProcessorDelayMatchesEngineDefault() {
384397
assertThat(this.properties.getTomcat().getBackgroundProcessorDelay())

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* @author Victor Mandujano
6060
* @author Parviz Rozikov
6161
* @author Moritz Halbritter
62+
* @author Yanming Zhou
6263
*/
6364
class TomcatWebServerFactoryCustomizerTests {
6465

@@ -194,6 +195,13 @@ void customMaxHttpRequestHeaderSize() {
194195
.isEqualTo(DataSize.ofMegabytes(10).toBytes()));
195196
}
196197

198+
@Test
199+
void customMaxParameterCount() {
200+
bind("server.tomcat.max-parameter-count=100");
201+
customizeAndRunServer(
202+
(server) -> assertThat(server.getTomcat().getConnector().getMaxParameterCount()).isEqualTo(100));
203+
}
204+
197205
@Test
198206
void customMaxRequestHttpHeaderSizeIgnoredIfNegative() {
199207
bind("server.max-http-request-header-size=-1");

0 commit comments

Comments
 (0)