弹簧钥匙斗篷:获取用户 ID
2022-09-03 04:23:13
我开发了一个Spring Boot Webservice,并使用Keycloak进行访问管理。该网站将一些用户数据存储在数据库中。我尝试将这些数据与登录的用户连接。
目前,我将用户名与数据一起存储。但我喜欢存储用户ID而不是用户名。我该怎么做?
我试图通过这个来获得安全上下文:
@Bean
@Scope(scopeName = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public KeycloakSecurityContext getKeycloakSecurityContext() {
return ((KeycloakPrincipal<KeycloakSecurityContext>) getRequest().getUserPrincipal()).getKeycloakSecurityContext();
}
但是我收到一个错误:
There was an unexpected error (type=Internal Server Error, status=500).
Error creating bean with name 'scopedTarget.getKeycloakSecurityContext'
defined in com.SiteApplication: Bean instantiation via factory method
failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [org.keycloak.KeycloakSecurityContext]: Factory method
'getKeycloakSecurityContext' threw exception; nested exception is
java.lang.ClassCastException:
org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken
cannot be cast to org.keycloak.KeycloakPrincipal
这是正确的方法吗?缺少什么?
谢谢!