22
22
import org .junit .jupiter .api .Test ;
23
23
24
24
import org .springframework .beans .factory .annotation .Autowired ;
25
+ import org .springframework .context .annotation .Bean ;
25
26
import org .springframework .context .annotation .Configuration ;
27
+ import org .springframework .core .Ordered ;
28
+ import org .springframework .core .annotation .Order ;
26
29
import org .springframework .mock .web .MockFilterChain ;
27
30
import org .springframework .mock .web .MockHttpServletRequest ;
28
31
import org .springframework .mock .web .MockHttpServletResponse ;
32
35
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
33
36
import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
34
37
import org .springframework .security .web .FilterChainProxy ;
38
+ import org .springframework .security .web .SecurityFilterChain ;
35
39
import org .springframework .web .bind .annotation .RequestMapping ;
36
40
import org .springframework .web .bind .annotation .RestController ;
37
41
import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
@@ -166,6 +170,38 @@ public void requestMatcherWhensMvcMatcherServletPathInLambdaThenPathIsSecured()
166
170
assertThat (this .response .getStatus ()).isEqualTo (HttpServletResponse .SC_OK );
167
171
}
168
172
173
+ @ Test
174
+ public void requestMatcherWhenMultiMvcMatcherInLambdaThenAllPathsAreDenied () throws Exception {
175
+ loadConfig (MultiMvcMatcherInLambdaConfig .class );
176
+ this .request .setRequestURI ("/test-1" );
177
+ this .springSecurityFilterChain .doFilter (this .request , this .response , this .chain );
178
+ assertThat (this .response .getStatus ()).isEqualTo (HttpServletResponse .SC_UNAUTHORIZED );
179
+ setup ();
180
+ this .request .setRequestURI ("/test-2" );
181
+ this .springSecurityFilterChain .doFilter (this .request , this .response , this .chain );
182
+ assertThat (this .response .getStatus ()).isEqualTo (HttpServletResponse .SC_UNAUTHORIZED );
183
+ setup ();
184
+ this .request .setRequestURI ("/test-3" );
185
+ this .springSecurityFilterChain .doFilter (this .request , this .response , this .chain );
186
+ assertThat (this .response .getStatus ()).isEqualTo (HttpServletResponse .SC_UNAUTHORIZED );
187
+ }
188
+
189
+ @ Test
190
+ public void requestMatcherWhenMultiMvcMatcherThenAllPathsAreDenied () throws Exception {
191
+ loadConfig (MultiMvcMatcherConfig .class );
192
+ this .request .setRequestURI ("/test-1" );
193
+ this .springSecurityFilterChain .doFilter (this .request , this .response , this .chain );
194
+ assertThat (this .response .getStatus ()).isEqualTo (HttpServletResponse .SC_UNAUTHORIZED );
195
+ setup ();
196
+ this .request .setRequestURI ("/test-2" );
197
+ this .springSecurityFilterChain .doFilter (this .request , this .response , this .chain );
198
+ assertThat (this .response .getStatus ()).isEqualTo (HttpServletResponse .SC_UNAUTHORIZED );
199
+ setup ();
200
+ this .request .setRequestURI ("/test-3" );
201
+ this .springSecurityFilterChain .doFilter (this .request , this .response , this .chain );
202
+ assertThat (this .response .getStatus ()).isEqualTo (HttpServletResponse .SC_UNAUTHORIZED );
203
+ }
204
+
169
205
public void loadConfig (Class <?>... configs ) {
170
206
this .context = new AnnotationConfigWebApplicationContext ();
171
207
this .context .register (configs );
@@ -174,6 +210,101 @@ public void loadConfig(Class<?>... configs) {
174
210
this .context .getAutowireCapableBeanFactory ().autowireBean (this );
175
211
}
176
212
213
+ @ EnableWebSecurity
214
+ @ Configuration
215
+ @ EnableWebMvc
216
+ static class MultiMvcMatcherInLambdaConfig {
217
+
218
+ @ Bean
219
+ @ Order (Ordered .HIGHEST_PRECEDENCE )
220
+ SecurityFilterChain first (HttpSecurity http ) throws Exception {
221
+ // @formatter:off
222
+ http
223
+ .requestMatchers ((requests ) -> requests
224
+ .mvcMatchers ("/test-1" )
225
+ .mvcMatchers ("/test-2" )
226
+ .mvcMatchers ("/test-3" )
227
+ )
228
+ .authorizeRequests ((authorize ) -> authorize .anyRequest ().denyAll ())
229
+ .httpBasic (withDefaults ());
230
+ // @formatter:on
231
+ return http .build ();
232
+ }
233
+
234
+ @ Bean
235
+ SecurityFilterChain second (HttpSecurity http ) throws Exception {
236
+ // @formatter:off
237
+ http
238
+ .requestMatchers ((requests ) -> requests
239
+ .mvcMatchers ("/test-1" )
240
+ )
241
+ .authorizeRequests ((authorize ) -> authorize
242
+ .anyRequest ().permitAll ()
243
+ );
244
+ // @formatter:on
245
+ return http .build ();
246
+ }
247
+
248
+ @ RestController
249
+ static class PathController {
250
+
251
+ @ RequestMapping ({ "/test-1" , "/test-2" , "/test-3" })
252
+ String path () {
253
+ return "path" ;
254
+ }
255
+
256
+ }
257
+
258
+ }
259
+
260
+ @ EnableWebSecurity
261
+ @ Configuration
262
+ @ EnableWebMvc
263
+ static class MultiMvcMatcherConfig {
264
+
265
+ @ Bean
266
+ @ Order (Ordered .HIGHEST_PRECEDENCE )
267
+ SecurityFilterChain first (HttpSecurity http ) throws Exception {
268
+ // @formatter:off
269
+ http
270
+ .requestMatchers ()
271
+ .mvcMatchers ("/test-1" )
272
+ .mvcMatchers ("/test-2" )
273
+ .mvcMatchers ("/test-3" )
274
+ .and ()
275
+ .authorizeRequests ()
276
+ .anyRequest ().denyAll ()
277
+ .and ()
278
+ .httpBasic (withDefaults ());
279
+ // @formatter:on
280
+ return http .build ();
281
+ }
282
+
283
+ @ Bean
284
+ SecurityFilterChain second (HttpSecurity http ) throws Exception {
285
+ // @formatter:off
286
+ http
287
+ .requestMatchers ()
288
+ .mvcMatchers ("/test-1" )
289
+ .and ()
290
+ .authorizeRequests ()
291
+ .anyRequest ().permitAll ();
292
+ // @formatter:on
293
+ return http .build ();
294
+ }
295
+
296
+ @ RestController
297
+ static class PathController {
298
+
299
+ @ RequestMapping ({ "/test-1" , "/test-2" , "/test-3" })
300
+ String path () {
301
+ return "path" ;
302
+ }
303
+
304
+ }
305
+
306
+ }
307
+
177
308
@ EnableWebSecurity
178
309
@ Configuration
179
310
@ EnableWebMvc
0 commit comments