在 Kotlin Flow 中使用 ReactiveSecurityContextHolder

2024-03-24

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

从我读到的来看,与SecurityContextHolder正在使用ThreadLocal来存储它,ReactiveSecurityContextHolder依赖于这样一个事实:Spring 在订阅我的反应链时,还将该上下文存储在该链中,从而允许我调用ReactiveSecurityContextHolder.getContext()从链条内部。

问题是我必须改变我的Mono<SecurityContext>在某个时刻进入心流,这让我失去了我的SecurityContext。所以我的问题是:有没有办法让 Spring Boot 控制器在检索安全上下文时返回 FlowReactiveSecurityContextHolder在我的逻辑里面?基本上,简化后,它应该看起来像这样:

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

请注意,如果我直接返回 Flux(跳过.asFlow()),或者添加一个.single() or .toList()最后(因此使用suspend fun),然后它工作正常并且返回我的安全上下文,但这又不是我想要的。我想解决方案是从 Flux 传输上下文(初始反应链来自ReactiveSecurityContextHolder)到流程,但默认情况下似乎没有完成。

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


您真正想要实现的是从 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()
}

如果您需要从挂起方法访问 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>"))
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Kotlin Flow 中使用 ReactiveSecurityContextHolder 的相关文章

随机推荐