diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfiguration.java index f91fa1ec8a..9bac3910ac 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,7 @@ import org.springframework.web.servlet.DispatcherServlet; * @author Greg Turnquist * @author Josh Long * @author Toshiaki Maki + * @author Yanming Zhou * @since 2.0.0 */ @AutoConfiguration @@ -72,6 +73,7 @@ public class MultipartAutoConfiguration { public StandardServletMultipartResolver multipartResolver() { StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver(); multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily()); + multipartResolver.setStrictServletCompliance(this.multipartProperties.isStrictServletCompliance()); return multipartResolver; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java index 1388ae73bb..4db7ec2e1c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,7 @@ import org.springframework.util.unit.DataSize; * @author Josh Long * @author Toshiaki Maki * @author Stephane Nicoll + * @author Yanming Zhou * @since 2.0.0 */ @ConfigurationProperties(prefix = "spring.servlet.multipart", ignoreUnknownFields = false) @@ -79,6 +80,12 @@ public class MultipartProperties { */ private boolean resolveLazily = false; + /** + * Whether to resolve the multipart request strictly comply with the Servlet specification, + * only kicking in for "multipart/form-data" requests. + */ + private boolean strictServletCompliance = false; + public boolean getEnabled() { return this.enabled; } @@ -127,6 +134,14 @@ public class MultipartProperties { this.resolveLazily = resolveLazily; } + public boolean isStrictServletCompliance() { + return this.strictServletCompliance; + } + + public void setStrictServletCompliance(boolean strictServletCompliance) { + this.strictServletCompliance = strictServletCompliance; + } + /** * Create a new {@link MultipartConfigElement} using the properties. * @return a new {@link MultipartConfigElement} configured using there properties diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfigurationTests.java index f60ffd3a5e..208a647ac0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfigurationTests.java @@ -64,6 +64,7 @@ import static org.mockito.Mockito.mock; * @author Josh Long * @author Ivan Sopov * @author Toshiaki Maki + * @author Yanming Zhou */ @DirtiesUrlFactories class MultipartAutoConfigurationTests { @@ -174,6 +175,17 @@ class MultipartAutoConfigurationTests { assertThat(multipartResolver).hasFieldOrPropertyWithValue("resolveLazily", true); } + @Test + void configureStrictServletCompliance() { + this.context = new AnnotationConfigServletWebServerApplicationContext(); + TestPropertyValues.of("spring.servlet.multipart.strict-servlet-compliance=true").applyTo(this.context); + this.context.register(WebServerWithNothing.class, BaseConfiguration.class); + this.context.refresh(); + StandardServletMultipartResolver multipartResolver = this.context + .getBean(StandardServletMultipartResolver.class); + assertThat(multipartResolver).hasFieldOrPropertyWithValue("strictServletCompliance", true); + } + @Test void configureMultipartProperties() { this.context = new AnnotationConfigServletWebServerApplicationContext();