Skip to content

Commit 3b7f714

Browse files
earlgrey02jzheaux
authored andcommitted
Add SecurityContextRepository to Kotlin Reactive DSL
1 parent 9a71442 commit 3b7f714

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

config/src/main/kotlin/org/springframework/security/config/web/server/ServerHttpSecurityDsl.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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,6 +19,7 @@ package org.springframework.security.config.web.server
1919
import org.springframework.security.authentication.ReactiveAuthenticationManager
2020
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository
2121
import org.springframework.security.web.server.SecurityWebFilterChain
22+
import org.springframework.security.web.server.context.ServerSecurityContextRepository
2223
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher
2324
import org.springframework.web.server.ServerWebExchange
2425
import org.springframework.web.server.WebFilter
@@ -65,6 +66,7 @@ operator fun ServerHttpSecurity.invoke(httpConfiguration: ServerHttpSecurityDsl.
6566
class ServerHttpSecurityDsl(private val http: ServerHttpSecurity, private val init: ServerHttpSecurityDsl.() -> Unit) {
6667

6768
var authenticationManager: ReactiveAuthenticationManager? = null
69+
var securityContextRepository: ServerSecurityContextRepository? = null
6870

6971
/**
7072
* Allows configuring the [ServerHttpSecurity] to only be invoked when matching the
@@ -718,6 +720,7 @@ class ServerHttpSecurityDsl(private val http: ServerHttpSecurity, private val in
718720
internal fun build(): SecurityWebFilterChain {
719721
init()
720722
authenticationManager?.also { this.http.authenticationManager(authenticationManager) }
723+
securityContextRepository?.also { this.http.securityContextRepository(securityContextRepository) }
721724
return this.http.build()
722725
}
723726
}

config/src/test/kotlin/org/springframework/security/config/web/server/ServerHttpSecurityDslTests.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ import org.springframework.security.config.test.SpringTestContextExtension
3535
import org.springframework.security.core.Authentication
3636
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter
3737
import org.springframework.security.web.server.SecurityWebFilterChain
38+
import org.springframework.security.web.server.context.NoOpServerSecurityContextRepository
3839
import org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter
40+
import org.springframework.security.web.server.context.ServerSecurityContextRepository
3941
import org.springframework.security.web.server.header.ContentTypeOptionsServerHttpHeadersWriter
4042
import org.springframework.security.web.server.header.StrictTransportSecurityServerHttpHeadersWriter
4143
import org.springframework.security.web.server.header.XFrameOptionsServerHttpHeadersWriter
@@ -251,4 +253,31 @@ class ServerHttpSecurityDslTests {
251253
return Mono.empty()
252254
}
253255
}
256+
257+
@Test
258+
fun `security context repository when configured in DSL then used`() {
259+
this.spring.register(SecurityContextRepositoryConfig::class.java).autowire()
260+
mockkObject(SecurityContextRepositoryConfig.SECURITY_CONTEXT_REPOSITORY)
261+
every {
262+
SecurityContextRepositoryConfig.SECURITY_CONTEXT_REPOSITORY.load(any())
263+
} returns Mono.empty()
264+
this.client.get().uri("/").exchange()
265+
verify(exactly = 1) { SecurityContextRepositoryConfig.SECURITY_CONTEXT_REPOSITORY.load(any()) }
266+
}
267+
268+
@Configuration
269+
@EnableWebFlux
270+
@EnableWebFluxSecurity
271+
open class SecurityContextRepositoryConfig {
272+
companion object {
273+
val SECURITY_CONTEXT_REPOSITORY: ServerSecurityContextRepository = NoOpServerSecurityContextRepository.getInstance()
274+
}
275+
276+
@Bean
277+
open fun springWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
278+
return http {
279+
securityContextRepository = SECURITY_CONTEXT_REPOSITORY
280+
}
281+
}
282+
}
254283
}

0 commit comments

Comments
 (0)