のどちらかが、私は何かが欠けてるか、それがどのように動作するかこれは...つまり
ですが、私はUserDetailsService
を実装し、サブ分類(AppUser
以下)春のユーティリティクラスUser
、(それがUserDetails
を実装します)。それが重要な場合は、それはこのようなものになる:春のセキュリティは、ログイン失敗時にUserDetailsの代わりにStringをprincipalとして返しますか?
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
// try loading user by its name
SystemUser user = null;
try {
user = this.sysUserService.getByUsername(username);
if(user == null)
throw new UsernameNotFoundException("User not found!");
}
catch(Exception e) {
throw new DataRetrievalFailureException(
"Could not load user with username: " + username);
}
// load user rights, and create UserDetails instance
UserDetails res = new AppUser(user, getUserAuthorities(user));
return res;
}
をそれから私は、このアプローチを使用してアカウントのロックを実装してみました:から主要なオブジェクトを取得しようとしているときに
public class LoginFailureEventListenter implements
ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
// rest omitted for brevity
@Override
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
// ...
SystemUser user = ((AppUser)event.getAuthentication().getPrincipal()).getSystemUser();
// cast exception - how is it possible to obtain String
// instead of UserDetails object here ?
// ...
}
}
はしかし、私はjava.lang.ClassCastException
に走りました(主オブジェクトのタイプはString
でした)。私は、問題を解決するために私のSystemUser
を再度ロードすることができますが、私はこれを期待していませんでした。
このシナリオでは、getPrincipal()
がgetPrincipal()
インスタンスを返すべきであるとソースドキュメントでも考えています。
考えますか?
これは実際にはそうなっている方法です...?私はすでに 'AuthenticationSuccessHandler'をカウンタリセットに使用しています。これが正しい方法であるかどうかを知ることはできません。' AuthenticationFailureHandler'を試してみるのは私には起こりませんでした。 – Less
はい、その通りです。イベントは「これは失敗した認証要求です」と表示され、認証要求にはユーザーの文字列識別子のみが含まれています。 –