Skip to content

Commit 6ecbd8d

Browse files
committed
Auto-Configure FormContentFilter in Spring MVC
Because `HttpPutFormContentFilter` has been deprecated in Spring Framework 5.1, this commit updates the auto-configuration to replace it with the new `FormContentFilter`. This new filter is building on the previous one and supports HTTP DELETE requests as well. Both filters should not be used in addition, so the former configuration has been removed. This commit also adds configuration metadata to let developers know about the configuration key change. Closes: gh-13363
1 parent 9a94fb3 commit 6ecbd8d

File tree

6 files changed

+41
-32
lines changed

6 files changed

+41
-32
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
import org.springframework.boot.autoconfigure.web.ResourceProperties.Strategy;
5656
import org.springframework.boot.autoconfigure.web.format.WebConversionService;
5757
import org.springframework.boot.context.properties.EnableConfigurationProperties;
58+
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
5859
import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter;
59-
import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter;
6060
import org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter;
6161
import org.springframework.context.ApplicationContext;
6262
import org.springframework.context.ResourceLoaderAware;
@@ -90,8 +90,8 @@
9090
import org.springframework.web.context.request.NativeWebRequest;
9191
import org.springframework.web.context.request.RequestAttributes;
9292
import org.springframework.web.context.request.RequestContextListener;
93+
import org.springframework.web.filter.FormContentFilter;
9394
import org.springframework.web.filter.HiddenHttpMethodFilter;
94-
import org.springframework.web.filter.HttpPutFormContentFilter;
9595
import org.springframework.web.filter.RequestContextFilter;
9696
import org.springframework.web.servlet.DispatcherServlet;
9797
import org.springframework.web.servlet.HandlerExceptionResolver;
@@ -160,10 +160,10 @@ public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
160160
}
161161

162162
@Bean
163-
@ConditionalOnMissingBean(HttpPutFormContentFilter.class)
164-
@ConditionalOnProperty(prefix = "spring.mvc.formcontent.putfilter", name = "enabled", matchIfMissing = true)
165-
public OrderedHttpPutFormContentFilter httpPutFormContentFilter() {
166-
return new OrderedHttpPutFormContentFilter();
163+
@ConditionalOnMissingBean(FormContentFilter.class)
164+
@ConditionalOnProperty(prefix = "spring.mvc.formcontent.filter", name = "enabled", matchIfMissing = true)
165+
public OrderedFormContentFilter formContentFilter() {
166+
return new OrderedFormContentFilter();
167167
}
168168

169169
// Defined as a nested config to ensure WebMvcConfigurer is not read when not

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,16 @@
464464
"name": "spring.mvc.formcontent.putfilter.enabled",
465465
"type": "java.lang.Boolean",
466466
"description": "Whether to enable Spring's HttpPutFormContentFilter.",
467+
"defaultValue": true,
468+
"deprecation" : {
469+
"replacement" : "spring.mvc.formcontent.filter.enabled",
470+
"level" : "error"
471+
}
472+
},
473+
{
474+
"name": "spring.mvc.formcontent.filter.enabled",
475+
"type": "java.lang.Boolean",
476+
"description": "Whether to enable Spring's FormContentFilter.",
467477
"defaultValue": true
468478
},
469479
{

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.
@@ -33,8 +33,8 @@
3333
import org.springframework.boot.test.util.TestPropertyValues;
3434
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
3535
import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor;
36+
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
3637
import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter;
37-
import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter;
3838
import org.springframework.context.annotation.Bean;
3939
import org.springframework.context.annotation.Configuration;
4040
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@@ -222,8 +222,8 @@ public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
222222
}
223223

224224
@Bean
225-
public OrderedHttpPutFormContentFilter httpPutFormContentFilter() {
226-
return new OrderedHttpPutFormContentFilter();
225+
public OrderedFormContentFilter formContentFilter() {
226+
return new OrderedFormContentFilter();
227227
}
228228

229229
}

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import org.springframework.boot.test.context.runner.ContextConsumer;
4646
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
4747
import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor;
48-
import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter;
48+
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
4949
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
5050
import org.springframework.context.ApplicationContext;
5151
import org.springframework.context.annotation.Bean;
@@ -71,8 +71,8 @@
7171
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
7272
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
7373
import org.springframework.web.context.request.ServletWebRequest;
74+
import org.springframework.web.filter.FormContentFilter;
7475
import org.springframework.web.filter.HiddenHttpMethodFilter;
75-
import org.springframework.web.filter.HttpPutFormContentFilter;
7676
import org.springframework.web.servlet.HandlerAdapter;
7777
import org.springframework.web.servlet.HandlerExceptionResolver;
7878
import org.springframework.web.servlet.HandlerMapping;
@@ -551,27 +551,26 @@ public void customMediaTypes() {
551551
}
552552

553553
@Test
554-
public void httpPutFormContentFilterIsAutoConfigured() {
554+
public void formContentFilterIsAutoConfigured() {
555555
this.contextRunner.run((context) -> assertThat(context)
556-
.hasSingleBean(OrderedHttpPutFormContentFilter.class));
556+
.hasSingleBean(OrderedFormContentFilter.class));
557557
}
558558

559559
@Test
560-
public void httpPutFormContentFilterCanBeOverridden() {
561-
this.contextRunner.withUserConfiguration(CustomHttpPutFormContentFilter.class)
560+
public void formContentFilterCanBeOverridden() {
561+
this.contextRunner.withUserConfiguration(CustomFormContentFilter.class)
562562
.run((context) -> {
563-
assertThat(context)
564-
.doesNotHaveBean(OrderedHttpPutFormContentFilter.class);
565-
assertThat(context).hasSingleBean(HttpPutFormContentFilter.class);
563+
assertThat(context).doesNotHaveBean(OrderedFormContentFilter.class);
564+
assertThat(context).hasSingleBean(FormContentFilter.class);
566565
});
567566
}
568567

569568
@Test
570-
public void httpPutFormContentFilterCanBeDisabled() {
569+
public void formContentFilterCanBeDisabled() {
571570
this.contextRunner
572-
.withPropertyValues("spring.mvc.formcontent.putfilter.enabled=false")
571+
.withPropertyValues("spring.mvc.formcontent.filter.enabled=false")
573572
.run((context) -> assertThat(context)
574-
.doesNotHaveBean(HttpPutFormContentFilter.class));
573+
.doesNotHaveBean(FormContentFilter.class));
575574
}
576575

577576
@Test
@@ -1076,11 +1075,11 @@ private static class CustomWebBindingInitializer
10761075
}
10771076

10781077
@Configuration
1079-
static class CustomHttpPutFormContentFilter {
1078+
static class CustomFormContentFilter {
10801079

10811080
@Bean
1082-
public HttpPutFormContentFilter customHttpPutFormContentFilter() {
1083-
return new HttpPutFormContentFilter();
1081+
public FormContentFilter customFormContentFilter() {
1082+
return new FormContentFilter();
10841083
}
10851084

10861085
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ content into your application. Rather, pick only the properties that you need.
428428
spring.mvc.dispatch-trace-request=false # Whether to dispatch TRACE requests to the FrameworkServlet doService method.
429429
spring.mvc.dispatch-options-request=true # Whether to dispatch OPTIONS requests to the FrameworkServlet doService method.
430430
spring.mvc.favicon.enabled=true # Whether to enable resolution of favicon.ico.
431-
spring.mvc.formcontent.putfilter.enabled=true # Whether to enable Spring's HttpPutFormContentFilter.
431+
spring.mvc.formcontent.filter.enabled=true # Whether to enable Spring's FormContentFilter.
432432
spring.mvc.hiddenmethod.filter.enabled=true # Whether to enable Spring's HiddenHttpMethodFilter.
433433
spring.mvc.ignore-default-model-on-redirect=true # Whether the content of the "default" model should be ignored during redirect scenarios.
434434
spring.mvc.locale= # Locale to use. By default, this locale is overridden by the "Accept-Language" header.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.
@@ -18,16 +18,16 @@
1818

1919
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2020
import org.springframework.core.Ordered;
21-
import org.springframework.web.filter.HttpPutFormContentFilter;
21+
import org.springframework.web.filter.FormContentFilter;
2222

2323
/**
24-
* {@link HttpPutFormContentFilter} that also implements {@link Ordered}.
24+
* {@link FormContentFilter} that also implements {@link Ordered}.
2525
*
2626
* @author Joao Pedro Evangelista
27-
* @since 2.0.0
27+
* @author Brian Clozel
28+
* @since 2.1.0
2829
*/
29-
public class OrderedHttpPutFormContentFilter extends HttpPutFormContentFilter
30-
implements Ordered {
30+
public class OrderedFormContentFilter extends FormContentFilter implements Ordered {
3131

3232
/**
3333
* Higher order to ensure the filter is applied before Spring Security.

0 commit comments

Comments
 (0)