V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
winRain
V2EX  ›  程序员

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

  •  
  •   winRain · 2021-11-23 10:40:37 +08:00 · 1182 次点击
    这是一个创建于 875 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    各位大佬帮帮忙。

    3 条回复    2021-11-24 09:52:06 +08:00
    Oktfolio
        1
    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
        2
    damai0419  
       2021-11-23 12:41:25 +08:00
    keycloak 没用过。
    但 spring cloud gateway 和 security 结合的时候,是在 ServerSecurityContextRepository 的子类中进行转换操作的。
    winRain
        3
    winRain  
    OP
       2021-11-24 09:52:06 +08:00
    谢谢两位老哥,我仔细看了下源码,已经找到原因了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5988 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:08 · PVG 14:08 · LAX 23:08 · JFK 02:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.