在 Kotlin Flow 中使用 ReactiveSecurityContextHolder

Sim*_*onH 7 spring kotlin spring-boot spring-webflux kotlin-coroutines

我正在使用 Kotlin 开发 Spring Boot (2.2) 项目,使用 CouchDB 作为(反应式)数据库,因此使用异步 DAO(挂起函数或返回 Flow 的函数)。我正在尝试设置 WebFlux 以便也拥有异步控制器(同样,我想返回 Flows,而不是 Flux)。但我在从 检索我的安全上下文时遇到了麻烦ReactiveSecurityContextHolder

从我读到的内容来看,与SecurityContextHolder使用它来ThreadLocal存储它不同,ReactiveSecurityContextHolderSpring 在订阅我的反应链时,还将该上下文存储在该链内,从而允许我ReactiveSecurityContextHolder.getContext()从链内进行调用。

问题是我必须Mono<SecurityContext>在某个时候将我的转换为流程,这使我失去了我的SecurityContext. 所以我的问题是:有没有办法让 Spring Boot 控制器返回 Flow,同时从ReactiveSecurityContextHolder我的逻辑内部检索安全上下文?基本上,简化后,它应该看起来像这样:

@GetMapping
fun getArticles(): Flow<String> {
    return ReactiveSecurityContextHolder.getContext().flux().asFlow() // returns nothing
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果我直接返回 Flux(跳过.asFlow()),或者在末尾添加.single()or (因此使用 a ),那么它可以正常工作并且返回我的安全上下文,但这又不是我想要的。我猜解决方案是将上下文从 Flux(来自 的初始反应链)转移到 Flow,但默认情况下似乎没有这样做。.toList()suspend funReactiveSecurityContextHolder

编辑:这是一个展示问题的示例项目:https://github.com/Simon3/webflux-kotlin-sample

小智 7

您真正想要实现的是从 Flow 内部访问您的 ReactorContext。

实现此目的的一种方法是放宽返回 Flow 的需要并返回 Flux。这允许您恢复 ReactorContext 并将其传递给您将用于生成数据的 Flow。

@ExperimentalCoroutinesApi
@GetMapping("/flow")
fun flow(): Flux<Map<String, String>> = Mono.subscriberContext().flatMapMany { reactorCtx ->
    flow {
        val ctx = coroutineContext[ReactorContext.Key]?.context?.get<Mono<SecurityContext>>(SecurityContext::class.java)?.asFlow()?.single()
        emit(mapOf("user" to ((ctx?.authentication?.principal as? User)?.username ?: "<NONE>")))
    }.flowOn(reactorCtx.asCoroutineContext()).asFlux()
}
Run Code Online (Sandbox Code Playgroud)

如果您需要从挂起方法访问 ReactorContext,您可以简单地从 coroutineContext 获取它,无需进一步的技巧:

@ExperimentalCoroutinesApi
@GetMapping("/suspend")
suspend fun suspend(): Map<String,String> {
    val ctx = coroutineContext[ReactorContext.Key]?.context?.get<Mono<SecurityContext>>(SecurityContext::class.java)?.asFlow()?.single()
    return mapOf("user" to ((ctx?.authentication?.principal as? User)?.username ?: "<NONE>"))
}
Run Code Online (Sandbox Code Playgroud)