求助帖: Spring Security 如何将 token 转换成 Authentication 类?

2021-11-23 10:40:37 +08:00
 winRain

彦祖们,我最近在用 keycloak 做认证,后端使用 spring security 和 keycloak spring security adapter 进行安全保护。

当我使用 client 从 keycloak 获取到了 token ,用 token 去调用后端 api 的时候,在 KeycloakAuthenticationProcessingFilter 180 行中,是直接去 SecurityContext中拿到了未认证的 Authentication 。

我想知道 token 是什么时候转换成了 Authentication 并放进 SecurityContext 中去的?我能不能通过实现子类修改逻辑?

各位大佬帮帮忙。

1196 次点击
所在节点    程序员
3 条回复
Oktfolio
2021-11-23 11:27:33 +08:00
多看看源码吧,能找到的。

class extends AbstractAuthenticationToken

AuthenticationFilter#attemptAuthentication(HttpServletRequest request, HttpServletResponse response)

AuthenticationFilter#successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication)
damai0419
2021-11-23 12:41:25 +08:00
keycloak 没用过。
但 spring cloud gateway 和 security 结合的时候,是在 ServerSecurityContextRepository 的子类中进行转换操作的。
winRain
2021-11-24 09:52:06 +08:00
谢谢两位老哥,我仔细看了下源码,已经找到原因了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/817319

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX