Spring Webflux + Spring Security Interaction - Why does the WebFilter run twice?
Hi,
My goal is to add the Spring Security Auth0 Id, to each request made to my Spring Webflux API.
For my project, I am using Spring Boot Webflux, as I'm also using R2DBC, so wanted to benefit from the non-blocking design, for my Rest API design.
What I tried:
-
Using AOP around my suspend controller functions so I could run a separate AOP function, that could add the Auth0 Id to the coroutine created by that controller. Why add the variable to the coroutine context? So I don't have to add a parameter all the way through the chain (from Controller > Service > Repository > SQL binding). This didn't work, as AOP does not suppose kotlin coroutine suspend functions.
-
Instead I tried adding a WebFilter, that adds the Security Context Auth0 Id, to the co-routine. This ALSO did not work, as the coroutine context created by the webfilter (see below code), does not flow through to the co-routine used by the controller (a Spring Boot Webflux internal that I don't quite understand).
-
So then I also saved the Auth0Id, to the Project Reactor Context (see the line .contextWrite). This DID work. When I tried to fetch the Auth0 Id later on (just before creating the SQL query, down in the repository). But I did notice that when I added a println statement to the below code, it prints out twice? Is this a bug? Or just my lack of understanding?
(Yes, I admit, I am not a spring expert, but I'm also not a newbie either and I am keen to learn - I've been working with Spring Webflux and Spring Security for my project since last July)
Thanks in advance,
Sachin
Attempt to add Auth0 ID, to both Co-Routine Context, and Project Reactor Context (fall back) Goal also to try and use Kotlin Structured Concurrency (rather than Project Reactor, as far as possible)
/**
* WebFilter that sets up authentication context for both Coroutines and Reactor.
*
* This filter intercepts all incoming web requests and:
* 1. Extracts the authenticated user ID from Spring Security
* 2. Adds this ID to both Kotlin Coroutine Context and Project Reactor Context
* 3. Ensures all downstream code can access this ID regardless of context boundaries
*
* ## How This Works
*
* This filter makes the authenticated user ID automatically available throughout the
* application without having to manually pass it through parameters.
*
* ### The Request Flow:
*
* 1. User makes a request to the application
* 2. This filter gets the authenticated user ID from Spring Security
* 3. It sets this ID in both context systems:
* - Kotlin Coroutine Context - for suspend functions and coroutine-based code
* - Project Reactor Context - for reactive operators and WebFlux code
* 4. All application code (controllers, services, repositories) can access the user ID
* from either context system as needed
* 5. When the request completes, both contexts are automatically cleaned up
*
* ### Why Dual Contexts?
*
* Spring WebFlux combines reactive programming (Reactor) with Kotlin coroutines.
* When crossing boundaries between these systems, context information can be lost.
* By storing the auth user ID in both context systems, we ensure it's always
* available regardless of how a particular component accesses it.
*
* This approach eliminates the need to manually pass the user ID through method
* parameters, keeping controller methods clean and focused on business logic.
*/
@Component
internal class AuthUserContextFilter : WebFilter {
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return mono {
webFilterSuspended(exchange, chain)
}
}
/**
* Filters each incoming request to add auth user ID to both context systems.
*
* This method:
* 1. Gets the authenticated user from Spring Security's reactive context
* 2. Adds this user ID to the Kotlin Coroutine Context
* 3. Adds the same user ID to the Project Reactor Context
* 4. Processes the request with both contexts properly set up
*
* The dual-context approach ensures that the auth user ID is available
* regardless of:
* - Whether code is using coroutines or reactive programming
* - Coroutine boundaries where context might not be fully propagated
* - WebFlux internal operations that might rely on Reactor context
*
* Technical details for developers:
* - Uses ReactiveSecurityContextHolder for non-blocking auth access
* - Creates a coroutine using the 'mono' builder from kotlinx.coroutines.reactor
* - Sets up the coroutine context with 'withAuthUserId'
* - Sets up the Reactor context with 'contextWrite'
* - All database operations will be able to access this ID from either context
*
* @param exchange The current server exchange (contains request/response info)
* @param chain The filter chain to continue processing the request
* @return A Mono that completes when the request is finished
*/
private suspend fun webFilterSuspended(exchange: ServerWebExchange, chain: WebFilterChain): Void? {
println("Web Filter run")
val authUserId = ReactiveSecurityContextHolder.getContext()
.awaitSingleOrNull()
?.authentication
?.name
?: "12345" // Default if null
// Add user ID to the Coroutine Context
val authContext = coroutineContext.withAuthUserId(authUserId)
// Execute the rest of the chain within the enhanced coroutine context
return withContext(authContext) {
chain.filter(exchange)
// Add to Reactor context as well
// This is crucial for cross-boundary communication
// between coroutines and reactive code
.contextWrite { it.put("AUTH_USER_ID", authUserId) }
.awaitFirstOrNull()
}
}
}
Attempt to get the correct context, just before doing the Spring R2DBC binding
/**
* Database client wrapper that automatically adds the authenticated user ID to all SQL queries.
*
* This component wraps the standard Spring DatabaseClient and enhances it by automatically
* binding the auth_user_id parameter to every SQL query using a dual-context approach.
*
* ## Benefits
*
* - Eliminates repetitive auth_user_id binding code in repositories
* - Ensures auth_user_id is consistently applied to all relevant database operations
* - Reduces the chance of security issues from forgotten auth filters
* - Keeps repository code focused on business logic rather than auth concerns
* - Handles context propagation issues at reactive boundaries
*
* ## How It Works
*
* 1. The AuthCoroutineContextFilter sets up the user ID in both coroutine and Reactor contexts
* 2. The repositories/services inject and use this AuthDatabaseClient
* 3. When executing a query, this client tries to get the user ID from:
* - First, the Kotlin coroutine context
* - Then, if that fails, from the Project Reactor context
* 4. It adds this ID as a parameter named "auth_user_id" to your SQL
*
* This dual-context approach ensures the auth user ID is available regardless of
* reactive boundaries that might be crossed during request processing.
*
* ## Usage Example
*
* ```kotlin
* @Repository
* class UserRepository(private val authDbClient: AuthDatabaseClient) {
* suspend fun getUser(userId: Long): User {
* // No need to bind auth_user_id - it happens automatically
* return authDbClient.sql("""
* SELECT * FROM users
* WHERE auth_user_id = :auth_user_id AND id = :user_id
* """)
* .bind("user_id", userId)
* .map { row -> mapToUser(row) }
* .awaitSingle()
* }
* }
* ```
*/
@Component
class DatabaseClientBindAuth(private val databaseClient: DatabaseClient) {
/**
* Creates a SQL query with auth_user_id automatically bound from context.
*
* This method uses a dual-context approach to retrieve the authenticated user ID:
* 1. First tries to get it from the Kotlin coroutine context
* 2. If not found there, falls back to the Project Reactor context
* 3. Adds the ID as a bound parameter named "auth_user_id" to your SQL query
*
* The dual-context approach ensures the auth user ID is available regardless of
* how coroutines are managed or which reactive boundaries are crossed in your
* application, preventing "No auth user ID in context" errors.
*
* The caller doesn't need to manually bind this parameter, keeping repository
* code cleaner and ensuring the auth context is consistently applied.
*
* @param sql The SQL query to execute, with potential :auth_user_id placeholder
* @return A DatabaseClient.GenericExecuteSpec with auth_user_id already bound
*/
suspend fun sql(sql: String): DatabaseClient.GenericExecuteSpec {
// Try to get from coroutine context first
val fromCoroutine = coroutineContext.authUserId
// If not found, try to get from Reactor context
val authUserId = if (fromCoroutine != null) {
fromCoroutine
} else {
// Get from Reactor context
Mono.deferContextual { ctx ->
Mono.just(ctx.getOrDefault("AUTH_USER_ID", "system") as String)
}.awaitSingle()
}
// Create the SQL query with the auth user ID bound as a parameter
return databaseClient.sql(sql.trimMargin())
.bind("auth_user_id", authUserId)
}
}
The below is a bit redundant, as I can't get AOP and Co-routine context to work....
/**
* AuthCoroutineContext provides utilities for storing and retrieving the authenticated user ID
* within Kotlin coroutines.
*
* ## What is Coroutine Context?
*
* A CoroutineContext is like a map of elements that accompanies a coroutine throughout its lifecycle.
* It's similar to:
* - ThreadLocal variables in traditional threading, but for coroutines
* - HTTP request attributes in a web application
* - A backpack that a coroutine carries with it containing important information
*
* ## Lifecycle of Coroutine Context
*
* - Each coroutine has its own CoroutineContext
* - The context is created when the coroutine starts and destroyed when it completes
* - In a web application, this means the context is effectively request-scoped
* - New HTTP requests create new coroutines with fresh contexts
* - When a request is complete, the coroutine ends and its context is garbage collected
*
* ## How This Auth Context Works
*
* 1. When a request comes in, our filter adds the user ID to the coroutine context
* 2. This ID automatically "flows" with the coroutine through the application
* 3. Services and repositories can access this ID through extension properties
* 4. When the request completes, the context is automatically cleaned up
*
* This eliminates the need to pass user IDs through method parameters or use ThreadLocal
* variables which require manual cleanup.
*/
internal object AuthCoroutineContext {
/**
* Element that stores the authenticated user ID in a coroutine context.
*
* This is the actual data container that holds the user ID in the context map.
* Each coroutine will have its own instance of this element.
*
* @param userId The authenticated user's ID
*/
class AuthUserIdElement(val userId: String) : CoroutineContext.Element {
// The companion object implements Key, which serves as a unique identifier
// for looking up this type of element in the context map
companion object Key : CoroutineContext.Key<AuthUserIdElement>
// This links the element to its key for lookup in the context
override val key: CoroutineContext.Key<*> = Key
}
/**
* Extension property to easily retrieve the authenticated user ID from coroutine context.
*
* Usage:
* ```
* // Inside a coroutine or suspend function
* val userId = coroutineContext.authUserId
* ```
*
* @return The authenticated user ID, or null if not set
*/
val CoroutineContext.authUserId: String?
get() = this[AuthUserIdElement.Key]?.userId
/**
* Extension function to add an authenticated user ID to a coroutine context.
*
* This creates a new context with the user ID added, without modifying the original.
*
* Usage:
* ```
* // Create a new context with the auth user ID
* val newContext = coroutineContext.withAuthUserId("user123")
*
* // Launch a coroutine with this context
* withContext(newContext) {
* // The user ID is available in this coroutine
* }
* ```
*
* @param userId The authenticated user's ID to add to the context
* @return A new context with the user ID element added
*/
fun CoroutineContext.withAuthUserId(userId: String): CoroutineContext =
this + AuthUserIdElement(userId)
}
Did you find any solution on this? I'm getting the same behaviour but with this message: [org.springframework.web.util.pattern.PatternParseException: Not allowed to capture 'tenantId' twice in the same pattern] for HTTP POST "/api/v1/admin/api-keys/wetg3sf8eifj4wr", but ServerHttpResponse already committed, and I don't have any dupped routes