eti*_*iau 10 spring-security spring-webflux
你能解释处理@PreAuthorize("hasRole('ADMIN')")的建议在Reactive应用程序中检索SecurityContext的位置吗?
以下Spring Security示例很好地说明了这种用法:https://github.com/spring-projects/spring-security/tree/5.0.0.M4/samples/javaconfig/hellowebflux-method
在检查了Spring Security Webflux源代码之后,我发现了SecurityContextRepository的一些实现,但是load方法需要ServerWebExchange作为参数.
我试图了解如何在标准服务中替换SecurityContextHolder.getContext().getAuthentication()调用(因为ThreadLocal不再是Reactive Application中的选项),但我不明白如何用调用替换它到没有ServerWebExchange引用的SecurityContextRepository.
你是对的,ThreadLocal不再是一个选项,因为请求的处理不依赖于特定的线程.
目前,Spring Security将身份验证信息存储为ServerWebExchange属性,因此绑定到当前请求/响应对.但是,当您无法直接访问当前交易所时,您仍需要这些信息,例如@PreAuthorize.
认证信息存储在无管道本身(从您这么接近Mono或Flux),这是一个非常有趣的反应器的功能-管理依赖于特定的上下文Subscriber(在Web应用程序,HTTP客户端从服务器和行为提取数据因此).
我不知道SecurityContextHolder从上下文中获取身份验证信息的等效方法或快捷方法.
请参阅参考文档中有关Reactor Context功能的更多信息.您还可以在此处查看Spring Security中使用的示例.
小智 6
会ReactiveSecurityContextHolder以反应方式提供身份验证,类似于SecurityContextHolder。
它的getContext()方法提供了一个Mono<SecurityContext>,就像SecurityContextHolder.getContext()提供了一个SecurityContext。
ReactiveSecurityContextHolder
.getContext()
.map(context ->
context.getAuthentication()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3218 次 |
| 最近记录: |