Introduce Ordered Filter and WebFilter interfaces

Add `Ordered` variants of `javax.servlet.Filter` and
`org.springframework.web.server.WebFilter` mainly so that we can
deprecate `FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER`.

Closes gh-14793
pull/14744/merge
Phillip Webb 6 years ago
parent d6df7cf324
commit d102e0d7f7

@ -25,7 +25,7 @@ import java.util.UUID;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.DispatcherType;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.filter.OrderedFilter;
import org.springframework.core.Ordered;
import org.springframework.util.StringUtils;
@ -58,7 +58,7 @@ public class SecurityProperties implements SecurityPrerequisite {
* other filters registered with the container). There is no connection between this
* and the {@code @Order} on a WebSecurityConfigurer.
*/
public static final int DEFAULT_FILTER_ORDER = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
public static final int DEFAULT_FILTER_ORDER = OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER
- 100;
private final Filter filter = new Filter();

@ -32,7 +32,7 @@ import org.springframework.boot.autoconfigure.orm.jpa.test.City;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.filter.OrderedFilter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -85,8 +85,7 @@ public class SecurityAutoConfigurationTests {
.getBean("securityFilterChainRegistration",
DelegatingFilterProxyRegistrationBean.class)
.getOrder()).isEqualTo(
FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
- 100));
OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER - 100));
}
@Test
@ -126,8 +125,7 @@ public class SecurityAutoConfigurationTests {
.getBean("securityFilterChainRegistration",
DelegatingFilterProxyRegistrationBean.class)
.getOrder()).isEqualTo(
FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
- 100));
OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER - 100));
}
@Test

@ -3063,7 +3063,7 @@ If a specific order is required, you should avoid configuring a Filter that read
request body at `Ordered.HIGHEST_PRECEDENCE`, since it might go against the character
encoding configuration of your application. If a Servlet filter wraps the request, it
should be configured with an order that is less than or equal to
`FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER`.
`OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER`.

@ -26,12 +26,12 @@ import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
* @since 2.0.5
*/
public class OrderedHiddenHttpMethodFilter extends HiddenHttpMethodFilter
implements Ordered {
implements OrderedWebFilter {
/**
* The default order is high to ensure the filter is applied before Spring Security.
*/
public static final int DEFAULT_ORDER = -10000;
public static final int DEFAULT_ORDER = REQUEST_WRAPPER_FILTER_MAX_ORDER - 10000;
private int order = DEFAULT_ORDER;

@ -0,0 +1,35 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.web.reactive.filter;
import org.springframework.core.Ordered;
import org.springframework.web.server.WebFilter;
/**
* An {@link Ordered} {@link org.springframework.web.server.WebFilter}.
*
* @author Phillip Webb
* @since 2.1.0
*/
public interface OrderedWebFilter extends WebFilter, Ordered {
/**
* Filters that wrap the request should be ordered less than or equal to this.
*/
int REQUEST_WRAPPER_FILTER_MAX_ORDER = 0;
}

@ -48,7 +48,10 @@ public abstract class AbstractFilterRegistrationBean<T extends Filter>
/**
* Filters that wrap the servlet request should be ordered less than or equal to this.
* @deprecated since 2.1.0 in favor of
* {@code OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER}
*/
@Deprecated
protected static final int REQUEST_WRAPPER_FILTER_MAX_ORDER = 0;
private final Log logger = LogFactory.getLog(getClass());

@ -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.
@ -45,7 +45,10 @@ public class FilterRegistrationBean<T extends Filter>
/**
* Filters that wrap the servlet request should be ordered less than or equal to this.
* @deprecated since 2.1.0 in favor of
* {@code OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER}
*/
@Deprecated
public static final int REQUEST_WRAPPER_FILTER_MAX_ORDER = AbstractFilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER;
private T filter;

@ -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.
@ -26,7 +26,7 @@ import org.springframework.web.filter.CharacterEncodingFilter;
* @since 2.0.0
*/
public class OrderedCharacterEncodingFilter extends CharacterEncodingFilter
implements Ordered {
implements OrderedFilter {
private int order = Ordered.HIGHEST_PRECEDENCE;

@ -0,0 +1,36 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.web.servlet.filter;
import javax.servlet.Filter;
import org.springframework.core.Ordered;
/**
* An {@link Ordered} {@link javax.servlet.Filter}.
*
* @author Phillip Webb
* @since 2.1.0
*/
public interface OrderedFilter extends Filter, Ordered {
/**
* Filters that wrap the servlet request should be ordered less than or equal to this.
*/
int REQUEST_WRAPPER_FILTER_MAX_ORDER = 0;
}

@ -16,7 +16,6 @@
package org.springframework.boot.web.servlet.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.core.Ordered;
import org.springframework.web.filter.FormContentFilter;
@ -27,13 +26,12 @@ import org.springframework.web.filter.FormContentFilter;
* @author Brian Clozel
* @since 2.1.0
*/
public class OrderedFormContentFilter extends FormContentFilter implements Ordered {
public class OrderedFormContentFilter extends FormContentFilter implements OrderedFilter {
/**
* Higher order to ensure the filter is applied before Spring Security.
*/
public static final int DEFAULT_ORDER = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
- 9900;
public static final int DEFAULT_ORDER = REQUEST_WRAPPER_FILTER_MAX_ORDER - 9900;
private int order = DEFAULT_ORDER;

@ -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.
@ -16,7 +16,6 @@
package org.springframework.boot.web.servlet.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.core.Ordered;
import org.springframework.web.filter.HiddenHttpMethodFilter;
@ -27,13 +26,12 @@ import org.springframework.web.filter.HiddenHttpMethodFilter;
* @since 2.0.0
*/
public class OrderedHiddenHttpMethodFilter extends HiddenHttpMethodFilter
implements Ordered {
implements OrderedFilter {
/**
* The default order is high to ensure the filter is applied before Spring Security.
*/
public static final int DEFAULT_ORDER = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
- 10000;
public static final int DEFAULT_ORDER = REQUEST_WRAPPER_FILTER_MAX_ORDER - 10000;
private int order = DEFAULT_ORDER;

@ -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.
@ -16,7 +16,6 @@
package org.springframework.boot.web.servlet.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.core.Ordered;
import org.springframework.web.filter.RequestContextFilter;
@ -26,10 +25,11 @@ import org.springframework.web.filter.RequestContextFilter;
* @author Phillip Webb
* @since 2.0.0
*/
public class OrderedRequestContextFilter extends RequestContextFilter implements Ordered {
public class OrderedRequestContextFilter extends RequestContextFilter
implements OrderedFilter {
// Order defaults to after Spring Session filter
private int order = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER - 105;
private int order = REQUEST_WRAPPER_FILTER_MAX_ORDER - 105;
@Override
public int getOrder() {

@ -7,6 +7,8 @@
<suppress files=".+Configuration\.java" checks="HideUtilityClassConstructor" />
<suppress files=".+Application\.java" checks="HideUtilityClassConstructor" />
<suppress files="SignalUtils\.java" checks="IllegalImport" />
<suppress files="OrderedFilter\.java" checks="InterfaceIsType" />
<suppress files="OrderedWebFilter\.java" checks="InterfaceIsType" />
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]cli[\\/]command[\\/]" checks="ImportControl" />
<suppress files="[\\/]src[\\/]main[\\/]java[\\/]sample[\\/]" checks="ImportControl" />
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]sample[\\/]" checks="ImportControl" />

Loading…
Cancel
Save