【问题标题】:Should I explicitly verify Keycloak token or this is done by Keycloak adapter?我应该明确验证 Keycloak 令牌还是由 Keycloak 适配器完成?
【发布时间】:2020-02-11 20:26:46
【问题描述】:

有一个 Spring-boot REST API,需要由 Keycloak 保护,应用程序正在使用 Keycloak-Spring-Security 适配器 (6.0.1)。

对 API 端点的调用,携带从 Keycloak(目前通过邮递员)获得的不记名令牌。

我能够成功执行 REST 端点调用,但还有其他事情困扰着我 - 我是否应该根据公钥显式验证令牌?

1 - 适配器是否针对公钥执行令牌验证,还是我应该实施它?

2 - 如果适配器正在这样做 - 你能指出这是在哪些类中完成的吗?

3 - 如果 - 不是 - 应如何实施此验证?是否有任何 Keycloak 库可用于验证令牌?

【问题讨论】:

    标签: spring-boot spring-security oauth-2.0 keycloak bearer-token


    【解决方案1】:

    好吧,在网上搜索了几天的答案后 - 我明白了。 我查看了Keycloak-spring-security-adapter的代码,找到了。

    首先,我得到了用于调试的 keycloak 的日志记录杆:

    logging.level.org.keycloak=DEBUG
    

    然后我尝试使用错误令牌访问我的端点(我预计这会产生异常,即更可见的跟踪;它确实做到了):

        2019-10-17 10:18:57,905 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.PreAuthActionsHandler | adminRequest http://localhost:8081/error 
        2019-10-17 10:18:57,906 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter | Request is to process authentication 
        2019-10-17 10:18:57,906 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter | Attempting Keycloak authentication 
        2019-10-17 10:18:57,906 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.BearerTokenRequestAuthenticator | Found [1] values in authorization header, selecting the first value for Bearer. 
        2019-10-17 10:18:57,906 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.BearerTokenRequestAuthenticator | Verifying access_token 
        2019-10-17 10:18:57,908 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.BearerTokenRequestAuthenticator | Failed to verify token 
        2019-10-17 10:18:57,908 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.RequestAuthenticator | Bearer FAILED 
        2019-10-17 10:18:57,908 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter | Auth outcome: FAILED 
        2019-10-17 10:18:57,925 | 30860 | http-nio-8081-exec-2 |  |  |  |  | DEBUG |  | org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter | Authentication request failed: org.keycloak.adapters.springsecurity.KeycloakAuthenticationException: Invalid authorization header, see WWW-Authenticate header for details org.keycloak.adapters.springsecurity.KeycloakAuthenticationException: Invalid authorization header, see WWW-Authenticate header for details
            at org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter.attemptAuthentication(KeycloakAuthenticationProcessingFilter.java:158)
            at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
            at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
        .....
    

    从那时起,很明显令牌正在被验证,如果您查看参与的类,您会发现它在某些情况下正在根据公钥进行验证。

    在我的情况下 (bearer-only) 参与此身份验证和验证的类是:

    org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter
    org.keycloak.adapters.BearerTokenRequestAuthenticator
    

    希望这可以帮助像我这样的其他人在 Keycloak 中找到自己的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-18
      • 2020-04-02
      • 2018-06-24
      • 2018-09-09
      • 2021-08-16
      • 2019-04-08
      • 2017-09-13
      • 2017-01-03
      相关资源
      最近更新 更多