1
私はSpringブートWebサービスを開発し、アクセス管理にKeycloakを使用しました。 ウェブサイトには、いくつかのユーザーデータがデータベースに保存されています。これらのデータをログインしたユーザーと接続しようとしています。Spring Keycloak:ユーザーIDを取得
現在、私はユーザー名をデータに保存しています。しかし、私はユーザー名の代わりにユーザーIDを保存するのが好きです。どうやってやるの?
私はこれによってSecurityContextが取得してください:
@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
は、これは正しい方法ですか?何が欠けている?
ありがとうございました!