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