SpringBoot + WebFlux + Spring Security ,SecurityContextHolder.getContext().getAuthentication()获取不到当前用户

解决方案

直接在Controller接口方法参数中写入org.springframework.security.core.Authentication,如下:

@GetMapping(path = "/test") public Mono<Response<?,?>> test(Authentication authentication) { return Mono.just(ResponseUtil.success(authentication != null ? authentication.getPrincipal() : null)); }

通过log得到的结果是Controller执行线程和执行Fliter的线程不是同一个

后续使用

在调用如ReactiveUserDetailsPasswordService.updatePassword()等方法的前,执行SecurityContextHolder.getContext().setAuthentication(authentication);不然会报错。因为这些方法里面也调用了SecurityContextHolder.getContext().getAuthentication()


__EOF__

本文作者ZOLMK
本文链接https://www.cnblogs.com/zolmk/p/17338336.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zolmk  阅读(212)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示