From 6ecbd8d21b2581713195b135aa4006b4977209ec Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 17 Aug 2018 15:21:34 +0200 Subject: [PATCH] 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 --- .../web/servlet/WebMvcAutoConfiguration.java | 12 ++++---- ...itional-spring-configuration-metadata.json | 10 +++++++ .../HttpEncodingAutoConfigurationTests.java | 8 ++--- .../servlet/WebMvcAutoConfigurationTests.java | 29 +++++++++---------- .../appendix-application-properties.adoc | 2 +- ...ter.java => OrderedFormContentFilter.java} | 12 ++++---- 6 files changed, 41 insertions(+), 32 deletions(-) rename spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/{OrderedHttpPutFormContentFilter.java => OrderedFormContentFilter.java} (79%) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java index dfd39bbc80..2683a7200c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java @@ -55,8 +55,8 @@ import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.autoconfigure.web.ResourceProperties.Strategy; import org.springframework.boot.autoconfigure.web.format.WebConversionService; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter; import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter; -import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter; import org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter; import org.springframework.context.ApplicationContext; import org.springframework.context.ResourceLoaderAware; @@ -90,8 +90,8 @@ import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextListener; +import org.springframework.web.filter.FormContentFilter; import org.springframework.web.filter.HiddenHttpMethodFilter; -import org.springframework.web.filter.HttpPutFormContentFilter; import org.springframework.web.filter.RequestContextFilter; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.HandlerExceptionResolver; @@ -160,10 +160,10 @@ public class WebMvcAutoConfiguration { } @Bean - @ConditionalOnMissingBean(HttpPutFormContentFilter.class) - @ConditionalOnProperty(prefix = "spring.mvc.formcontent.putfilter", name = "enabled", matchIfMissing = true) - public OrderedHttpPutFormContentFilter httpPutFormContentFilter() { - return new OrderedHttpPutFormContentFilter(); + @ConditionalOnMissingBean(FormContentFilter.class) + @ConditionalOnProperty(prefix = "spring.mvc.formcontent.filter", name = "enabled", matchIfMissing = true) + public OrderedFormContentFilter formContentFilter() { + return new OrderedFormContentFilter(); } // Defined as a nested config to ensure WebMvcConfigurer is not read when not diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 2a55eec16d..5a45e710ae 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -464,6 +464,16 @@ "name": "spring.mvc.formcontent.putfilter.enabled", "type": "java.lang.Boolean", "description": "Whether to enable Spring's HttpPutFormContentFilter.", + "defaultValue": true, + "deprecation" : { + "replacement" : "spring.mvc.formcontent.filter.enabled", + "level" : "error" + } + }, + { + "name": "spring.mvc.formcontent.filter.enabled", + "type": "java.lang.Boolean", + "description": "Whether to enable Spring's FormContentFilter.", "defaultValue": true }, { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfigurationTests.java index cc65700888..ed2de567f7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -33,8 +33,8 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor; +import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter; import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter; -import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.AnnotationAwareOrderComparator; @@ -222,8 +222,8 @@ public class HttpEncodingAutoConfigurationTests { } @Bean - public OrderedHttpPutFormContentFilter httpPutFormContentFilter() { - return new OrderedHttpPutFormContentFilter(); + public OrderedFormContentFilter formContentFilter() { + return new OrderedFormContentFilter(); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index ee863d3da3..c92d8b5525 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -45,7 +45,7 @@ import org.springframework.boot.test.context.assertj.AssertableWebApplicationCon import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor; -import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter; +import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -71,8 +71,8 @@ import org.springframework.web.accept.ParameterContentNegotiationStrategy; import org.springframework.web.accept.PathExtensionContentNegotiationStrategy; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.context.request.ServletWebRequest; +import org.springframework.web.filter.FormContentFilter; import org.springframework.web.filter.HiddenHttpMethodFilter; -import org.springframework.web.filter.HttpPutFormContentFilter; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.HandlerMapping; @@ -551,27 +551,26 @@ public class WebMvcAutoConfigurationTests { } @Test - public void httpPutFormContentFilterIsAutoConfigured() { + public void formContentFilterIsAutoConfigured() { this.contextRunner.run((context) -> assertThat(context) - .hasSingleBean(OrderedHttpPutFormContentFilter.class)); + .hasSingleBean(OrderedFormContentFilter.class)); } @Test - public void httpPutFormContentFilterCanBeOverridden() { - this.contextRunner.withUserConfiguration(CustomHttpPutFormContentFilter.class) + public void formContentFilterCanBeOverridden() { + this.contextRunner.withUserConfiguration(CustomFormContentFilter.class) .run((context) -> { - assertThat(context) - .doesNotHaveBean(OrderedHttpPutFormContentFilter.class); - assertThat(context).hasSingleBean(HttpPutFormContentFilter.class); + assertThat(context).doesNotHaveBean(OrderedFormContentFilter.class); + assertThat(context).hasSingleBean(FormContentFilter.class); }); } @Test - public void httpPutFormContentFilterCanBeDisabled() { + public void formContentFilterCanBeDisabled() { this.contextRunner - .withPropertyValues("spring.mvc.formcontent.putfilter.enabled=false") + .withPropertyValues("spring.mvc.formcontent.filter.enabled=false") .run((context) -> assertThat(context) - .doesNotHaveBean(HttpPutFormContentFilter.class)); + .doesNotHaveBean(FormContentFilter.class)); } @Test @@ -1076,11 +1075,11 @@ public class WebMvcAutoConfigurationTests { } @Configuration - static class CustomHttpPutFormContentFilter { + static class CustomFormContentFilter { @Bean - public HttpPutFormContentFilter customHttpPutFormContentFilter() { - return new HttpPutFormContentFilter(); + public FormContentFilter customFormContentFilter() { + return new FormContentFilter(); } } diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index be9a0139e2..9b321b87d9 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -428,7 +428,7 @@ content into your application. Rather, pick only the properties that you need. spring.mvc.dispatch-trace-request=false # Whether to dispatch TRACE requests to the FrameworkServlet doService method. spring.mvc.dispatch-options-request=true # Whether to dispatch OPTIONS requests to the FrameworkServlet doService method. spring.mvc.favicon.enabled=true # Whether to enable resolution of favicon.ico. - spring.mvc.formcontent.putfilter.enabled=true # Whether to enable Spring's HttpPutFormContentFilter. + spring.mvc.formcontent.filter.enabled=true # Whether to enable Spring's FormContentFilter. spring.mvc.hiddenmethod.filter.enabled=true # Whether to enable Spring's HiddenHttpMethodFilter. spring.mvc.ignore-default-model-on-redirect=true # Whether the content of the "default" model should be ignored during redirect scenarios. spring.mvc.locale= # Locale to use. By default, this locale is overridden by the "Accept-Language" header. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedHttpPutFormContentFilter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedFormContentFilter.java similarity index 79% rename from spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedHttpPutFormContentFilter.java rename to spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedFormContentFilter.java index e3ca4fa9a6..8b42e16a50 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedHttpPutFormContentFilter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedFormContentFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -18,16 +18,16 @@ package org.springframework.boot.web.servlet.filter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.core.Ordered; -import org.springframework.web.filter.HttpPutFormContentFilter; +import org.springframework.web.filter.FormContentFilter; /** - * {@link HttpPutFormContentFilter} that also implements {@link Ordered}. + * {@link FormContentFilter} that also implements {@link Ordered}. * * @author Joao Pedro Evangelista - * @since 2.0.0 + * @author Brian Clozel + * @since 2.1.0 */ -public class OrderedHttpPutFormContentFilter extends HttpPutFormContentFilter - implements Ordered { +public class OrderedFormContentFilter extends FormContentFilter implements Ordered { /** * Higher order to ensure the filter is applied before Spring Security.