Skip to content

Commit 05b87c5

Browse files
quaffmhalbritter
authored andcommitted
Introduce configuration property for strict servlet compliance
The property is named spring.servlet.multipart.strict-servlet-compliance See gh-37242
1 parent 5a0f87f commit 05b87c5

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@
4646
* @author Greg Turnquist
4747
* @author Josh Long
4848
* @author Toshiaki Maki
49+
* @author Yanming Zhou
4950
* @since 2.0.0
5051
*/
5152
@AutoConfiguration
@@ -72,6 +73,7 @@ public MultipartConfigElement multipartConfigElement() {
7273
public StandardServletMultipartResolver multipartResolver() {
7374
StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();
7475
multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily());
76+
multipartResolver.setStrictServletCompliance(this.multipartProperties.isStrictServletCompliance());
7577
return multipartResolver;
7678
}
7779

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@
4343
* @author Josh Long
4444
* @author Toshiaki Maki
4545
* @author Stephane Nicoll
46+
* @author Yanming Zhou
4647
* @since 2.0.0
4748
*/
4849
@ConfigurationProperties(prefix = "spring.servlet.multipart", ignoreUnknownFields = false)
@@ -79,6 +80,12 @@ public class MultipartProperties {
7980
*/
8081
private boolean resolveLazily = false;
8182

83+
/**
84+
* Whether to resolve the multipart request strictly comply with the Servlet specification,
85+
* only kicking in for "multipart/form-data" requests.
86+
*/
87+
private boolean strictServletCompliance = false;
88+
8289
public boolean getEnabled() {
8390
return this.enabled;
8491
}
@@ -127,6 +134,14 @@ public void setResolveLazily(boolean resolveLazily) {
127134
this.resolveLazily = resolveLazily;
128135
}
129136

137+
public boolean isStrictServletCompliance() {
138+
return this.strictServletCompliance;
139+
}
140+
141+
public void setStrictServletCompliance(boolean strictServletCompliance) {
142+
this.strictServletCompliance = strictServletCompliance;
143+
}
144+
130145
/**
131146
* Create a new {@link MultipartConfigElement} using the properties.
132147
* @return a new {@link MultipartConfigElement} configured using there properties

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* @author Josh Long
6565
* @author Ivan Sopov
6666
* @author Toshiaki Maki
67+
* @author Yanming Zhou
6768
*/
6869
@DirtiesUrlFactories
6970
class MultipartAutoConfigurationTests {
@@ -174,6 +175,17 @@ void configureResolveLazily() {
174175
assertThat(multipartResolver).hasFieldOrPropertyWithValue("resolveLazily", true);
175176
}
176177

178+
@Test
179+
void configureStrictServletCompliance() {
180+
this.context = new AnnotationConfigServletWebServerApplicationContext();
181+
TestPropertyValues.of("spring.servlet.multipart.strict-servlet-compliance=true").applyTo(this.context);
182+
this.context.register(WebServerWithNothing.class, BaseConfiguration.class);
183+
this.context.refresh();
184+
StandardServletMultipartResolver multipartResolver = this.context
185+
.getBean(StandardServletMultipartResolver.class);
186+
assertThat(multipartResolver).hasFieldOrPropertyWithValue("strictServletCompliance", true);
187+
}
188+
177189
@Test
178190
void configureMultipartProperties() {
179191
this.context = new AnnotationConfigServletWebServerApplicationContext();

0 commit comments

Comments
 (0)