Skip to content

Add SecurityContextRepository to Kotlin Reactive DSL #15013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package org.springframework.security.config.web.server
import org.springframework.security.authentication.ReactiveAuthenticationManager
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository
import org.springframework.security.web.server.SecurityWebFilterChain
import org.springframework.security.web.server.context.ServerSecurityContextRepository
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher
import org.springframework.web.server.ServerWebExchange
import org.springframework.web.server.WebFilter
Expand Down Expand Up @@ -65,6 +66,7 @@ operator fun ServerHttpSecurity.invoke(httpConfiguration: ServerHttpSecurityDsl.
class ServerHttpSecurityDsl(private val http: ServerHttpSecurity, private val init: ServerHttpSecurityDsl.() -> Unit) {

var authenticationManager: ReactiveAuthenticationManager? = null
var securityContextRepository: ServerSecurityContextRepository? = null

/**
* Allows configuring the [ServerHttpSecurity] to only be invoked when matching the
Expand Down Expand Up @@ -718,6 +720,7 @@ class ServerHttpSecurityDsl(private val http: ServerHttpSecurity, private val in
internal fun build(): SecurityWebFilterChain {
init()
authenticationManager?.also { this.http.authenticationManager(authenticationManager) }
securityContextRepository?.also { this.http.securityContextRepository(securityContextRepository) }
return this.http.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import org.springframework.security.config.test.SpringTestContextExtension
import org.springframework.security.core.Authentication
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter
import org.springframework.security.web.server.SecurityWebFilterChain
import org.springframework.security.web.server.context.NoOpServerSecurityContextRepository
import org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter
import org.springframework.security.web.server.context.ServerSecurityContextRepository
import org.springframework.security.web.server.header.ContentTypeOptionsServerHttpHeadersWriter
import org.springframework.security.web.server.header.StrictTransportSecurityServerHttpHeadersWriter
import org.springframework.security.web.server.header.XFrameOptionsServerHttpHeadersWriter
Expand Down Expand Up @@ -251,4 +253,31 @@ class ServerHttpSecurityDslTests {
return Mono.empty()
}
}

@Test
fun `security context repository when configured in DSL then used`() {
this.spring.register(SecurityContextRepositoryConfig::class.java).autowire()
mockkObject(SecurityContextRepositoryConfig.SECURITY_CONTEXT_REPOSITORY)
every {
SecurityContextRepositoryConfig.SECURITY_CONTEXT_REPOSITORY.load(any())
} returns Mono.empty()
this.client.get().uri("/").exchange()
verify(exactly = 1) { SecurityContextRepositoryConfig.SECURITY_CONTEXT_REPOSITORY.load(any()) }
}

@Configuration
@EnableWebFlux
@EnableWebFluxSecurity
open class SecurityContextRepositoryConfig {
companion object {
val SECURITY_CONTEXT_REPOSITORY: ServerSecurityContextRepository = NoOpServerSecurityContextRepository.getInstance()
}

@Bean
open fun springWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http {
securityContextRepository = SECURITY_CONTEXT_REPOSITORY
}
}
}
}