我应该明确验证 Keycloak 令牌还是由 Keycloak 适配器完成?
Should I explicitly verify Keycloak token or this is done by Keycloak adapter?
有一个 Spring-boot REST API,需要通过 Keycloak 进行保护,该应用程序正在使用 Keycloak-Spring-安全适配器 (6.0.1)。
对 API 端点的调用,携带从 Keycloak(目前通过邮递员)获得的不记名令牌。
我能够执行成功的 REST 端点调用,但其他事情让我很困扰 - 我是否应该根据 public 密钥显式验证令牌?
1 - 适配器是否根据 public 密钥执行令牌验证,还是我应该执行它?
2 - 如果适配器正在执行此操作 - 你能指出 类 这是在哪完成的吗?
3 - 如果 - 不是 - 应该如何实施此验证?有没有我可以用来验证令牌的 Keycloak 库?
好吧,在网上搜索答案几天后 - 我明白了。
我查看了 Keycloak-spring-security-adapter 的代码并找到了它。
首先,我得到了用于 DEBUG 的 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)
.....
从那时起,很明显令牌正在被验证,如果你查看参与的 类,你会看到它正在根据 public 中的密钥进行验证某些情况。
类 参与此身份验证和验证的是,就我而言 (bearer-only
) 是:
org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter
org.keycloak.adapters.BearerTokenRequestAuthenticator
希望这可以帮助其他像我一样的人在 Keycloak 中找到自己的出路。
有一个 Spring-boot REST API,需要通过 Keycloak 进行保护,该应用程序正在使用 Keycloak-Spring-安全适配器 (6.0.1)。
对 API 端点的调用,携带从 Keycloak(目前通过邮递员)获得的不记名令牌。
我能够执行成功的 REST 端点调用,但其他事情让我很困扰 - 我是否应该根据 public 密钥显式验证令牌?
1 - 适配器是否根据 public 密钥执行令牌验证,还是我应该执行它?
2 - 如果适配器正在执行此操作 - 你能指出 类 这是在哪完成的吗?
3 - 如果 - 不是 - 应该如何实施此验证?有没有我可以用来验证令牌的 Keycloak 库?
好吧,在网上搜索答案几天后 - 我明白了。 我查看了 Keycloak-spring-security-adapter 的代码并找到了它。
首先,我得到了用于 DEBUG 的 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)
.....
从那时起,很明显令牌正在被验证,如果你查看参与的 类,你会看到它正在根据 public 中的密钥进行验证某些情况。
类 参与此身份验证和验证的是,就我而言 (bearer-only
) 是:
org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter
org.keycloak.adapters.BearerTokenRequestAuthenticator
希望这可以帮助其他像我一样的人在 Keycloak 中找到自己的出路。