• 请不要在回答技术问题时复制粘贴 AI 生成的内容
winRain
V2EX  ›  程序员

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

  •  
  •   winRain · Nov 23, 2021 · 1676 views
    This topic created in 1640 days ago, the information mentioned may be changed or developed.

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

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

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

    各位大佬帮帮忙。

    3 replies    2021-11-24 09:52:06 +08:00
    Oktfolio
        1
    Oktfolio  
       Nov 23, 2021
    多看看源码吧,能找到的。

    class extends AbstractAuthenticationToken

    AuthenticationFilter#attemptAuthentication(HttpServletRequest request, HttpServletResponse response)

    AuthenticationFilter#successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication)
    damai0419
        2
    damai0419  
       Nov 23, 2021
    keycloak 没用过。
    但 spring cloud gateway 和 security 结合的时候,是在 ServerSecurityContextRepository 的子类中进行转换操作的。
    winRain
        3
    winRain  
    OP
       Nov 24, 2021
    谢谢两位老哥,我仔细看了下源码,已经找到原因了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2818 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 15:15 · PVG 23:15 · LAX 08:15 · JFK 11:15
    ♥ Do have faith in what you're doing.