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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
Reference in New Issue
Block a user