Skip to content

Commit 48ac100

Browse files
committed
Remove WebSecurityConfigurerAdapter from Kotlin tests
Issue gh-10902
1 parent 736f439 commit 48ac100

39 files changed

+819
-493
lines changed

config/src/test/kotlin/org/springframework/security/config/annotation/web/AnonymousDslTests.kt

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -19,15 +19,16 @@ package org.springframework.security.config.annotation.web
1919
import org.junit.jupiter.api.Test
2020
import org.junit.jupiter.api.extension.ExtendWith
2121
import org.springframework.beans.factory.annotation.Autowired
22+
import org.springframework.context.annotation.Bean
2223
import org.springframework.security.authentication.AnonymousAuthenticationToken
2324
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2425
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
25-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
2626
import org.springframework.security.config.test.SpringTestContext
2727
import org.springframework.security.config.test.SpringTestContextExtension
2828
import org.springframework.security.core.annotation.AuthenticationPrincipal
2929
import org.springframework.security.core.authority.SimpleGrantedAuthority
3030
import org.springframework.security.core.context.SecurityContextHolder
31+
import org.springframework.security.web.SecurityFilterChain
3132
import org.springframework.test.web.servlet.MockMvc
3233
import org.springframework.test.web.servlet.get
3334
import org.springframework.web.bind.annotation.GetMapping
@@ -60,13 +61,15 @@ class AnonymousDslTests {
6061

6162
@EnableWebSecurity
6263
@EnableWebMvc
63-
open class PrincipalConfig : WebSecurityConfigurerAdapter() {
64-
override fun configure(http: HttpSecurity) {
64+
open class PrincipalConfig {
65+
@Bean
66+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
6567
http {
6668
anonymous {
6769
principal = "principal"
6870
}
6971
}
72+
return http.build()
7073
}
7174
}
7275

@@ -82,13 +85,15 @@ class AnonymousDslTests {
8285

8386
@EnableWebSecurity
8487
@EnableWebMvc
85-
open class KeyConfig : WebSecurityConfigurerAdapter() {
86-
override fun configure(http: HttpSecurity) {
88+
open class KeyConfig {
89+
@Bean
90+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
8791
http {
8892
anonymous {
8993
key = "key"
9094
}
9195
}
96+
return http.build()
9297
}
9398
}
9499

@@ -104,13 +109,15 @@ class AnonymousDslTests {
104109

105110
@EnableWebSecurity
106111
@EnableWebMvc
107-
open class AnonymousDisabledConfig : WebSecurityConfigurerAdapter() {
108-
override fun configure(http: HttpSecurity) {
112+
open class AnonymousDisabledConfig {
113+
@Bean
114+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
109115
http {
110116
anonymous {
111117
disable()
112118
}
113119
}
120+
return http.build()
114121
}
115122
}
116123

@@ -126,8 +133,9 @@ class AnonymousDslTests {
126133

127134
@EnableWebSecurity
128135
@EnableWebMvc
129-
open class AnonymousAuthoritiesConfig : WebSecurityConfigurerAdapter() {
130-
override fun configure(http: HttpSecurity) {
136+
open class AnonymousAuthoritiesConfig {
137+
@Bean
138+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
131139
http {
132140
anonymous {
133141
authorities = listOf(SimpleGrantedAuthority("TEST"))
@@ -136,6 +144,7 @@ class AnonymousDslTests {
136144
authorize(anyRequest, hasAuthority("TEST"))
137145
}
138146
}
147+
return http.build()
139148
}
140149
}
141150

config/src/test/kotlin/org/springframework/security/config/annotation/web/AuthorizeRequestsDslTests.kt

+41-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -24,14 +24,14 @@ import org.springframework.context.annotation.Configuration
2424
import org.springframework.http.HttpMethod
2525
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2626
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
27-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
2827
import org.springframework.security.config.test.SpringTestContext
2928
import org.springframework.security.config.test.SpringTestContextExtension
3029
import org.springframework.security.core.userdetails.User
3130
import org.springframework.security.core.userdetails.UserDetailsService
3231
import org.springframework.security.provisioning.InMemoryUserDetailsManager
3332
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
3433
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
34+
import org.springframework.security.web.SecurityFilterChain
3535
import org.springframework.security.web.util.matcher.RegexRequestMatcher
3636
import org.springframework.test.web.servlet.MockMvc
3737
import org.springframework.test.web.servlet.get
@@ -96,8 +96,9 @@ class AuthorizeRequestsDslTests {
9696
}
9797

9898
@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 {
101102
http {
102103
authorizeRequests {
103104
authorize(RegexRequestMatcher("/path", null), permitAll)
@@ -106,6 +107,7 @@ class AuthorizeRequestsDslTests {
106107
authorize(RegexRequestMatcher(".*", null), authenticated)
107108
}
108109
}
110+
return http.build()
109111
}
110112

111113
@RestController
@@ -152,14 +154,16 @@ class AuthorizeRequestsDslTests {
152154

153155
@EnableWebSecurity
154156
@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 {
157160
http {
158161
authorizeRequests {
159162
authorize("/path", permitAll)
160163
authorize("/**", authenticated)
161164
}
162165
}
166+
return http.build()
163167
}
164168

165169
@RestController
@@ -194,13 +198,15 @@ class AuthorizeRequestsDslTests {
194198

195199
@EnableWebSecurity
196200
@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 {
199204
http {
200205
authorizeRequests {
201206
authorize("/user/{userName}", "#userName == 'user'")
202207
}
203208
}
209+
return http.build()
204210
}
205211

206212
@RestController
@@ -235,14 +241,16 @@ class AuthorizeRequestsDslTests {
235241

236242
@EnableWebSecurity
237243
@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 {
240247
http {
241248
authorizeRequests {
242249
authorize("/**", hasRole("ADMIN"))
243250
}
244251
httpBasic { }
245252
}
253+
return http.build()
246254
}
247255

248256
@RestController
@@ -253,7 +261,7 @@ class AuthorizeRequestsDslTests {
253261
}
254262

255263
@Bean
256-
override fun userDetailsService(): UserDetailsService {
264+
open fun userDetailsService(): UserDetailsService {
257265
val userDetails = User.withDefaultPasswordEncoder()
258266
.username("user")
259267
.password("password")
@@ -298,14 +306,16 @@ class AuthorizeRequestsDslTests {
298306

299307
@EnableWebSecurity
300308
@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 {
303312
http {
304313
authorizeRequests {
305314
authorize("/**", hasAnyRole("ADMIN", "USER"))
306315
}
307316
httpBasic { }
308317
}
318+
return http.build()
309319
}
310320

311321
@RestController
@@ -316,7 +326,7 @@ class AuthorizeRequestsDslTests {
316326
}
317327

318328
@Bean
319-
override fun userDetailsService(): UserDetailsService {
329+
open fun userDetailsService(): UserDetailsService {
320330
val userDetails = User.withDefaultPasswordEncoder()
321331
.username("user")
322332
.password("password")
@@ -366,14 +376,16 @@ class AuthorizeRequestsDslTests {
366376

367377
@EnableWebSecurity
368378
@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 {
371382
http {
372383
authorizeRequests {
373384
authorize("/**", hasAnyAuthority("ROLE_ADMIN", "ROLE_USER"))
374385
}
375386
httpBasic { }
376387
}
388+
return http.build()
377389
}
378390

379391
@RestController
@@ -384,7 +396,7 @@ class AuthorizeRequestsDslTests {
384396
}
385397

386398
@Bean
387-
override fun userDetailsService(): UserDetailsService {
399+
open fun userDetailsService(): UserDetailsService {
388400
val userDetails = User.withDefaultPasswordEncoder()
389401
.username("user")
390402
.password("password")
@@ -425,15 +437,17 @@ class AuthorizeRequestsDslTests {
425437

426438
@EnableWebSecurity
427439
@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 {
430443
http {
431444
authorizeRequests {
432445
authorize("/path",
433446
"/spring",
434447
denyAll)
435448
}
436449
}
450+
return http.build()
437451
}
438452

439453
@RestController
@@ -446,14 +460,16 @@ class AuthorizeRequestsDslTests {
446460

447461
@EnableWebSecurity
448462
@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 {
451466
http {
452467
authorizeRequests {
453468
authorize(HttpMethod.GET, "/path", permitAll)
454469
authorize(HttpMethod.PUT, "/path", denyAll)
455470
}
456471
}
472+
return http.build()
457473
}
458474

459475
@RestController
@@ -481,14 +497,16 @@ class AuthorizeRequestsDslTests {
481497

482498
@EnableWebSecurity
483499
@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 {
486503
http {
487504
authorizeRequests {
488505
authorize(HttpMethod.GET, "/path", "/spring", denyAll)
489506
authorize(HttpMethod.PUT, "/path", "/spring", denyAll)
490507
}
491508
}
509+
return http.build()
492510
}
493511

494512
@RestController

0 commit comments

Comments
 (0)