Skip to content

Commit d102e0d

Browse files
committed
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
1 parent d6df7cf commit d102e0d

File tree

13 files changed

+99
-26
lines changed

13 files changed

+99
-26
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import org.springframework.boot.context.properties.ConfigurationProperties;
2727
import org.springframework.boot.web.servlet.DispatcherType;
28-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
28+
import org.springframework.boot.web.servlet.filter.OrderedFilter;
2929
import org.springframework.core.Ordered;
3030
import org.springframework.util.StringUtils;
3131

@@ -58,7 +58,7 @@ public class SecurityProperties implements SecurityPrerequisite {
5858
* other filters registered with the container). There is no connection between this
5959
* and the {@code @Order} on a WebSecurityConfigurer.
6060
*/
61-
public static final int DEFAULT_FILTER_ORDER = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
61+
public static final int DEFAULT_FILTER_ORDER = OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER
6262
- 100;
6363

6464
private final Filter filter = new Filter();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3333
import org.springframework.boot.test.rule.OutputCapture;
3434
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
35-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
35+
import org.springframework.boot.web.servlet.filter.OrderedFilter;
3636
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3737
import org.springframework.context.annotation.Bean;
3838
import org.springframework.context.annotation.Configuration;
@@ -85,8 +85,7 @@ public void testDefaultFilterOrderWithSecurityAdapter() {
8585
.getBean("securityFilterChainRegistration",
8686
DelegatingFilterProxyRegistrationBean.class)
8787
.getOrder()).isEqualTo(
88-
FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
89-
- 100));
88+
OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER - 100));
9089
}
9190

9291
@Test
@@ -126,8 +125,7 @@ public void testDefaultFilterOrder() {
126125
.getBean("securityFilterChainRegistration",
127126
DelegatingFilterProxyRegistrationBean.class)
128127
.getOrder()).isEqualTo(
129-
FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
130-
- 100));
128+
OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER - 100));
131129
}
132130

133131
@Test

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3063,7 +3063,7 @@ If a specific order is required, you should avoid configuring a Filter that read
30633063
request body at `Ordered.HIGHEST_PRECEDENCE`, since it might go against the character
30643064
encoding configuration of your application. If a Servlet filter wraps the request, it
30653065
should be configured with an order that is less than or equal to
3066-
`FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER`.
3066+
`OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER`.
30673067

30683068

30693069

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/filter/OrderedHiddenHttpMethodFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
* @since 2.0.5
2727
*/
2828
public class OrderedHiddenHttpMethodFilter extends HiddenHttpMethodFilter
29-
implements Ordered {
29+
implements OrderedWebFilter {
3030

3131
/**
3232
* The default order is high to ensure the filter is applied before Spring Security.
3333
*/
34-
public static final int DEFAULT_ORDER = -10000;
34+
public static final int DEFAULT_ORDER = REQUEST_WRAPPER_FILTER_MAX_ORDER - 10000;
3535

3636
private int order = DEFAULT_ORDER;
3737

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.reactive.filter;
18+
19+
import org.springframework.core.Ordered;
20+
import org.springframework.web.server.WebFilter;
21+
22+
/**
23+
* An {@link Ordered} {@link org.springframework.web.server.WebFilter}.
24+
*
25+
* @author Phillip Webb
26+
* @since 2.1.0
27+
*/
28+
public interface OrderedWebFilter extends WebFilter, Ordered {
29+
30+
/**
31+
* Filters that wrap the request should be ordered less than or equal to this.
32+
*/
33+
int REQUEST_WRAPPER_FILTER_MAX_ORDER = 0;
34+
35+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public abstract class AbstractFilterRegistrationBean<T extends Filter>
4848

4949
/**
5050
* Filters that wrap the servlet request should be ordered less than or equal to this.
51+
* @deprecated since 2.1.0 in favor of
52+
* {@code OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER}
5153
*/
54+
@Deprecated
5255
protected static final int REQUEST_WRAPPER_FILTER_MAX_ORDER = 0;
5356

5457
private final Log logger = LogFactory.getLog(getClass());

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

Lines changed: 4 additions & 1 deletion
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.
@@ -45,7 +45,10 @@ public class FilterRegistrationBean<T extends Filter>
4545

4646
/**
4747
* Filters that wrap the servlet request should be ordered less than or equal to this.
48+
* @deprecated since 2.1.0 in favor of
49+
* {@code OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER}
4850
*/
51+
@Deprecated
4952
public static final int REQUEST_WRAPPER_FILTER_MAX_ORDER = AbstractFilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER;
5053

5154
private T filter;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedCharacterEncodingFilter.java

Lines changed: 2 additions & 2 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.
@@ -26,7 +26,7 @@
2626
* @since 2.0.0
2727
*/
2828
public class OrderedCharacterEncodingFilter extends CharacterEncodingFilter
29-
implements Ordered {
29+
implements OrderedFilter {
3030

3131
private int order = Ordered.HIGHEST_PRECEDENCE;
3232

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.servlet.filter;
18+
19+
import javax.servlet.Filter;
20+
21+
import org.springframework.core.Ordered;
22+
23+
/**
24+
* An {@link Ordered} {@link javax.servlet.Filter}.
25+
*
26+
* @author Phillip Webb
27+
* @since 2.1.0
28+
*/
29+
public interface OrderedFilter extends Filter, Ordered {
30+
31+
/**
32+
* Filters that wrap the servlet request should be ordered less than or equal to this.
33+
*/
34+
int REQUEST_WRAPPER_FILTER_MAX_ORDER = 0;
35+
36+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedFormContentFilter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.web.servlet.filter;
1818

19-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2019
import org.springframework.core.Ordered;
2120
import org.springframework.web.filter.FormContentFilter;
2221

@@ -27,13 +26,12 @@
2726
* @author Brian Clozel
2827
* @since 2.1.0
2928
*/
30-
public class OrderedFormContentFilter extends FormContentFilter implements Ordered {
29+
public class OrderedFormContentFilter extends FormContentFilter implements OrderedFilter {
3130

3231
/**
3332
* Higher order to ensure the filter is applied before Spring Security.
3433
*/
35-
public static final int DEFAULT_ORDER = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
36-
- 9900;
34+
public static final int DEFAULT_ORDER = REQUEST_WRAPPER_FILTER_MAX_ORDER - 9900;
3735

3836
private int order = DEFAULT_ORDER;
3937

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedHiddenHttpMethodFilter.java

Lines changed: 3 additions & 5 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.web.servlet.filter;
1818

19-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2019
import org.springframework.core.Ordered;
2120
import org.springframework.web.filter.HiddenHttpMethodFilter;
2221

@@ -27,13 +26,12 @@
2726
* @since 2.0.0
2827
*/
2928
public class OrderedHiddenHttpMethodFilter extends HiddenHttpMethodFilter
30-
implements Ordered {
29+
implements OrderedFilter {
3130

3231
/**
3332
* The default order is high to ensure the filter is applied before Spring Security.
3433
*/
35-
public static final int DEFAULT_ORDER = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
36-
- 10000;
34+
public static final int DEFAULT_ORDER = REQUEST_WRAPPER_FILTER_MAX_ORDER - 10000;
3735

3836
private int order = DEFAULT_ORDER;
3937

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/OrderedRequestContextFilter.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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.web.servlet.filter;
1818

19-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2019
import org.springframework.core.Ordered;
2120
import org.springframework.web.filter.RequestContextFilter;
2221

@@ -26,10 +25,11 @@
2625
* @author Phillip Webb
2726
* @since 2.0.0
2827
*/
29-
public class OrderedRequestContextFilter extends RequestContextFilter implements Ordered {
28+
public class OrderedRequestContextFilter extends RequestContextFilter
29+
implements OrderedFilter {
3030

3131
// Order defaults to after Spring Session filter
32-
private int order = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER - 105;
32+
private int order = REQUEST_WRAPPER_FILTER_MAX_ORDER - 105;
3333

3434
@Override
3535
public int getOrder() {

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<suppress files=".+Configuration\.java" checks="HideUtilityClassConstructor" />
88
<suppress files=".+Application\.java" checks="HideUtilityClassConstructor" />
99
<suppress files="SignalUtils\.java" checks="IllegalImport" />
10+
<suppress files="OrderedFilter\.java" checks="InterfaceIsType" />
11+
<suppress files="OrderedWebFilter\.java" checks="InterfaceIsType" />
1012
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]cli[\\/]command[\\/]" checks="ImportControl" />
1113
<suppress files="[\\/]src[\\/]main[\\/]java[\\/]sample[\\/]" checks="ImportControl" />
1214
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]sample[\\/]" checks="ImportControl" />

0 commit comments

Comments
 (0)