1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
37
37
import org .springframework .security .web .access .ExceptionTranslationFilter ;
38
38
import org .springframework .security .web .access .channel .ChannelProcessingFilter ;
39
39
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
40
+ import org .springframework .security .web .context .SecurityContextPersistenceFilter ;
40
41
import org .springframework .security .web .context .request .async .WebAsyncManagerIntegrationFilter ;
42
+ import org .springframework .security .web .header .HeaderWriterFilter ;
41
43
42
44
import static org .assertj .core .api .Assertions .assertThat ;
43
45
@@ -70,6 +72,46 @@ public void addFilterAtWhenSameFilterDifferentPlacesThenOrderCorrect() {
70
72
ExceptionTranslationFilter .class );
71
73
}
72
74
75
+ @ Test
76
+ public void addFilterAfterWhenAfterCustomFilterThenOrderCorrect () {
77
+ this .spring .register (MyOtherFilterRelativeToMyFilterAfterConfig .class ).autowire ();
78
+
79
+ assertThatFilters ().containsSubsequence (WebAsyncManagerIntegrationFilter .class , MyFilter .class ,
80
+ MyOtherFilter .class );
81
+ }
82
+
83
+ @ Test
84
+ public void addFilterBeforeWhenBeforeCustomFilterThenOrderCorrect () {
85
+ this .spring .register (MyOtherFilterRelativeToMyFilterBeforeConfig .class ).autowire ();
86
+
87
+ assertThatFilters ().containsSubsequence (MyOtherFilter .class , MyFilter .class ,
88
+ WebAsyncManagerIntegrationFilter .class );
89
+ }
90
+
91
+ @ Test
92
+ public void addFilterAtWhenAtCustomFilterThenOrderCorrect () {
93
+ this .spring .register (MyOtherFilterRelativeToMyFilterAtConfig .class ).autowire ();
94
+
95
+ assertThatFilters ().containsSubsequence (WebAsyncManagerIntegrationFilter .class , MyFilter .class ,
96
+ MyOtherFilter .class , SecurityContextPersistenceFilter .class );
97
+ }
98
+
99
+ @ Test
100
+ public void addFilterBeforeWhenCustomFilterDifferentPlacesThenOrderCorrect () {
101
+ this .spring .register (MyOtherFilterBeforeToMyFilterMultipleAfterConfig .class ).autowire ();
102
+
103
+ assertThatFilters ().containsSubsequence (WebAsyncManagerIntegrationFilter .class , MyOtherFilter .class ,
104
+ MyFilter .class , ExceptionTranslationFilter .class );
105
+ }
106
+
107
+ @ Test
108
+ public void addFilterBeforeAndAfterWhenCustomFiltersDifferentPlacesThenOrderCorrect () {
109
+ this .spring .register (MyAnotherFilterRelativeToMyCustomFiltersMultipleConfig .class ).autowire ();
110
+
111
+ assertThatFilters ().containsSubsequence (HeaderWriterFilter .class , MyFilter .class , MyOtherFilter .class ,
112
+ MyOtherFilter .class , MyAnotherFilter .class , MyFilter .class , ExceptionTranslationFilter .class );
113
+ }
114
+
73
115
private ListAssert <Class <?>> assertThatFilters () {
74
116
FilterChainProxy filterChain = this .spring .getContext ().getBean (FilterChainProxy .class );
75
117
List <Class <?>> filters = filterChain .getFilters ("/" ).stream ().map (Object ::getClass )
@@ -87,6 +129,26 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
87
129
88
130
}
89
131
132
+ static class MyOtherFilter implements Filter {
133
+
134
+ @ Override
135
+ public void doFilter (ServletRequest servletRequest , ServletResponse servletResponse , FilterChain filterChain )
136
+ throws IOException , ServletException {
137
+ filterChain .doFilter (servletRequest , servletResponse );
138
+ }
139
+
140
+ }
141
+
142
+ static class MyAnotherFilter implements Filter {
143
+
144
+ @ Override
145
+ public void doFilter (ServletRequest servletRequest , ServletResponse servletResponse , FilterChain filterChain )
146
+ throws IOException , ServletException {
147
+ filterChain .doFilter (servletRequest , servletResponse );
148
+ }
149
+
150
+ }
151
+
90
152
@ EnableWebSecurity
91
153
static class MyFilterMultipleAfterConfig extends WebSecurityConfigurerAdapter {
92
154
@@ -129,4 +191,78 @@ protected void configure(HttpSecurity http) throws Exception {
129
191
130
192
}
131
193
194
+ @ EnableWebSecurity
195
+ static class MyOtherFilterRelativeToMyFilterAfterConfig extends WebSecurityConfigurerAdapter {
196
+
197
+ @ Override
198
+ protected void configure (HttpSecurity http ) throws Exception {
199
+ // @formatter:off
200
+ http
201
+ .addFilterAfter (new MyFilter (), WebAsyncManagerIntegrationFilter .class )
202
+ .addFilterAfter (new MyOtherFilter (), MyFilter .class );
203
+ // @formatter:on
204
+ }
205
+
206
+ }
207
+
208
+ @ EnableWebSecurity
209
+ static class MyOtherFilterRelativeToMyFilterBeforeConfig extends WebSecurityConfigurerAdapter {
210
+
211
+ @ Override
212
+ protected void configure (HttpSecurity http ) throws Exception {
213
+ // @formatter:off
214
+ http
215
+ .addFilterBefore (new MyFilter (), WebAsyncManagerIntegrationFilter .class )
216
+ .addFilterBefore (new MyOtherFilter (), MyFilter .class );
217
+ // @formatter:on
218
+ }
219
+
220
+ }
221
+
222
+ @ EnableWebSecurity
223
+ static class MyOtherFilterRelativeToMyFilterAtConfig extends WebSecurityConfigurerAdapter {
224
+
225
+ @ Override
226
+ protected void configure (HttpSecurity http ) throws Exception {
227
+ // @formatter:off
228
+ http
229
+ .addFilterAt (new MyFilter (), WebAsyncManagerIntegrationFilter .class )
230
+ .addFilterAt (new MyOtherFilter (), MyFilter .class );
231
+ // @formatter:on
232
+ }
233
+
234
+ }
235
+
236
+ @ EnableWebSecurity
237
+ static class MyOtherFilterBeforeToMyFilterMultipleAfterConfig extends WebSecurityConfigurerAdapter {
238
+
239
+ @ Override
240
+ protected void configure (HttpSecurity http ) throws Exception {
241
+ // @formatter:off
242
+ http
243
+ .addFilterAfter (new MyFilter (), WebAsyncManagerIntegrationFilter .class )
244
+ .addFilterAfter (new MyFilter (), ExceptionTranslationFilter .class )
245
+ .addFilterBefore (new MyOtherFilter (), MyFilter .class );
246
+ // @formatter:on
247
+ }
248
+
249
+ }
250
+
251
+ @ EnableWebSecurity
252
+ static class MyAnotherFilterRelativeToMyCustomFiltersMultipleConfig extends WebSecurityConfigurerAdapter {
253
+
254
+ @ Override
255
+ protected void configure (HttpSecurity http ) throws Exception {
256
+ // @formatter:off
257
+ http
258
+ .addFilterAfter (new MyFilter (), HeaderWriterFilter .class )
259
+ .addFilterBefore (new MyOtherFilter (), ExceptionTranslationFilter .class )
260
+ .addFilterAfter (new MyOtherFilter (), MyFilter .class )
261
+ .addFilterAt (new MyAnotherFilter (), MyOtherFilter .class )
262
+ .addFilterAfter (new MyFilter (), MyAnotherFilter .class );
263
+ // @formatter:on
264
+ }
265
+
266
+ }
267
+
132
268
}
0 commit comments