1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2022 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.
@@ -24,14 +24,14 @@ import org.springframework.context.annotation.Configuration
24
24
import org.springframework.http.HttpMethod
25
25
import org.springframework.security.config.annotation.web.builders.HttpSecurity
26
26
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
27
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
28
27
import org.springframework.security.config.test.SpringTestContext
29
28
import org.springframework.security.config.test.SpringTestContextExtension
30
29
import org.springframework.security.core.userdetails.User
31
30
import org.springframework.security.core.userdetails.UserDetailsService
32
31
import org.springframework.security.provisioning.InMemoryUserDetailsManager
33
32
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
34
33
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
34
+ import org.springframework.security.web.SecurityFilterChain
35
35
import org.springframework.security.web.util.matcher.RegexRequestMatcher
36
36
import org.springframework.test.web.servlet.MockMvc
37
37
import org.springframework.test.web.servlet.get
@@ -96,8 +96,9 @@ class AuthorizeRequestsDslTests {
96
96
}
97
97
98
98
@EnableWebSecurity
99
- open class AuthorizeRequestsByRegexConfig : WebSecurityConfigurerAdapter () {
100
- override fun configure (http : HttpSecurity ) {
99
+ open class AuthorizeRequestsByRegexConfig {
100
+ @Bean
101
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
101
102
http {
102
103
authorizeRequests {
103
104
authorize(RegexRequestMatcher (" /path" , null ), permitAll)
@@ -106,6 +107,7 @@ class AuthorizeRequestsDslTests {
106
107
authorize(RegexRequestMatcher (" .*" , null ), authenticated)
107
108
}
108
109
}
110
+ return http.build()
109
111
}
110
112
111
113
@RestController
@@ -152,14 +154,16 @@ class AuthorizeRequestsDslTests {
152
154
153
155
@EnableWebSecurity
154
156
@EnableWebMvc
155
- open class AuthorizeRequestsByMvcConfig : WebSecurityConfigurerAdapter () {
156
- override fun configure (http : HttpSecurity ) {
157
+ open class AuthorizeRequestsByMvcConfig {
158
+ @Bean
159
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
157
160
http {
158
161
authorizeRequests {
159
162
authorize(" /path" , permitAll)
160
163
authorize(" /**" , authenticated)
161
164
}
162
165
}
166
+ return http.build()
163
167
}
164
168
165
169
@RestController
@@ -194,13 +198,15 @@ class AuthorizeRequestsDslTests {
194
198
195
199
@EnableWebSecurity
196
200
@EnableWebMvc
197
- open class MvcMatcherPathVariablesConfig : WebSecurityConfigurerAdapter () {
198
- override fun configure (http : HttpSecurity ) {
201
+ open class MvcMatcherPathVariablesConfig {
202
+ @Bean
203
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
199
204
http {
200
205
authorizeRequests {
201
206
authorize(" /user/{userName}" , " #userName == 'user'" )
202
207
}
203
208
}
209
+ return http.build()
204
210
}
205
211
206
212
@RestController
@@ -235,14 +241,16 @@ class AuthorizeRequestsDslTests {
235
241
236
242
@EnableWebSecurity
237
243
@EnableWebMvc
238
- open class HasRoleConfig : WebSecurityConfigurerAdapter () {
239
- override fun configure (http : HttpSecurity ) {
244
+ open class HasRoleConfig {
245
+ @Bean
246
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
240
247
http {
241
248
authorizeRequests {
242
249
authorize(" /**" , hasRole(" ADMIN" ))
243
250
}
244
251
httpBasic { }
245
252
}
253
+ return http.build()
246
254
}
247
255
248
256
@RestController
@@ -253,7 +261,7 @@ class AuthorizeRequestsDslTests {
253
261
}
254
262
255
263
@Bean
256
- override fun userDetailsService (): UserDetailsService {
264
+ open fun userDetailsService (): UserDetailsService {
257
265
val userDetails = User .withDefaultPasswordEncoder()
258
266
.username(" user" )
259
267
.password(" password" )
@@ -298,14 +306,16 @@ class AuthorizeRequestsDslTests {
298
306
299
307
@EnableWebSecurity
300
308
@EnableWebMvc
301
- open class HasAnyRoleConfig : WebSecurityConfigurerAdapter () {
302
- override fun configure (http : HttpSecurity ) {
309
+ open class HasAnyRoleConfig {
310
+ @Bean
311
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
303
312
http {
304
313
authorizeRequests {
305
314
authorize(" /**" , hasAnyRole(" ADMIN" , " USER" ))
306
315
}
307
316
httpBasic { }
308
317
}
318
+ return http.build()
309
319
}
310
320
311
321
@RestController
@@ -316,7 +326,7 @@ class AuthorizeRequestsDslTests {
316
326
}
317
327
318
328
@Bean
319
- override fun userDetailsService (): UserDetailsService {
329
+ open fun userDetailsService (): UserDetailsService {
320
330
val userDetails = User .withDefaultPasswordEncoder()
321
331
.username(" user" )
322
332
.password(" password" )
@@ -366,14 +376,16 @@ class AuthorizeRequestsDslTests {
366
376
367
377
@EnableWebSecurity
368
378
@EnableWebMvc
369
- open class HasAnyAuthorityConfig : WebSecurityConfigurerAdapter () {
370
- override fun configure (http : HttpSecurity ) {
379
+ open class HasAnyAuthorityConfig {
380
+ @Bean
381
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
371
382
http {
372
383
authorizeRequests {
373
384
authorize(" /**" , hasAnyAuthority(" ROLE_ADMIN" , " ROLE_USER" ))
374
385
}
375
386
httpBasic { }
376
387
}
388
+ return http.build()
377
389
}
378
390
379
391
@RestController
@@ -384,7 +396,7 @@ class AuthorizeRequestsDslTests {
384
396
}
385
397
386
398
@Bean
387
- override fun userDetailsService (): UserDetailsService {
399
+ open fun userDetailsService (): UserDetailsService {
388
400
val userDetails = User .withDefaultPasswordEncoder()
389
401
.username(" user" )
390
402
.password(" password" )
@@ -425,15 +437,17 @@ class AuthorizeRequestsDslTests {
425
437
426
438
@EnableWebSecurity
427
439
@EnableWebMvc
428
- open class MvcMatcherServletPathConfig : WebSecurityConfigurerAdapter () {
429
- override fun configure (http : HttpSecurity ) {
440
+ open class MvcMatcherServletPathConfig {
441
+ @Bean
442
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
430
443
http {
431
444
authorizeRequests {
432
445
authorize(" /path" ,
433
446
" /spring" ,
434
447
denyAll)
435
448
}
436
449
}
450
+ return http.build()
437
451
}
438
452
439
453
@RestController
@@ -446,14 +460,16 @@ class AuthorizeRequestsDslTests {
446
460
447
461
@EnableWebSecurity
448
462
@EnableWebMvc
449
- open class AuthorizeRequestsByMvcConfigWithHttpMethod : WebSecurityConfigurerAdapter () {
450
- override fun configure (http : HttpSecurity ) {
463
+ open class AuthorizeRequestsByMvcConfigWithHttpMethod {
464
+ @Bean
465
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
451
466
http {
452
467
authorizeRequests {
453
468
authorize(HttpMethod .GET , " /path" , permitAll)
454
469
authorize(HttpMethod .PUT , " /path" , denyAll)
455
470
}
456
471
}
472
+ return http.build()
457
473
}
458
474
459
475
@RestController
@@ -481,14 +497,16 @@ class AuthorizeRequestsDslTests {
481
497
482
498
@EnableWebSecurity
483
499
@EnableWebMvc
484
- open class MvcMatcherServletPathHttpMethodConfig : WebSecurityConfigurerAdapter () {
485
- override fun configure (http : HttpSecurity ) {
500
+ open class MvcMatcherServletPathHttpMethodConfig {
501
+ @Bean
502
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
486
503
http {
487
504
authorizeRequests {
488
505
authorize(HttpMethod .GET , " /path" , " /spring" , denyAll)
489
506
authorize(HttpMethod .PUT , " /path" , " /spring" , denyAll)
490
507
}
491
508
}
509
+ return http.build()
492
510
}
493
511
494
512
@RestController
0 commit comments