春のセキュリティを使用してユーザーの無効な試行を表示しようとしています.カスタム User クラスを使用して、ユーザー名とパスワード以外の追加のユーザー詳細を取得しています。ユーザーの無効な試行を更新するために、AuthenticationSuccessEventListener と AuthenticationFailureListener という 2 つのリスナー クラスを作成しました。
onApplicationEvent メソッドで、以下に示すようにカスタム ユーザー オブジェクト (CustomUserDetails) を取得しようとしています。
@Component
public class AuthenticationFailureListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
@Autowired
private ILoginDAO loginDAO ;
@Override
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
CustomUserDetails user = (CustomUserDetails)event.getAuthentication().getPrincipal();//I get ClassCastException here.
String strUserID = user.getUserID();
CustomUserDetails customUser = loginDAO.loadUserByUsername(strUserID);
if (customUser != null){
...
} } }
event.getAuthentication().getPrincipal() は文字列、つまりユーザー名を返します。これを CustomUserDetails (カスタム ユーザー クラス) にキャストしようとすると、エラーが発生します。
PS - ログインページにユーザー ID/パスワードを入力しているため、loadUserByUsername(strUserID) を含むすべてのメソッドのパラメーターとしてユーザー ID を渡します。
AuthenticationFailureBadCredentialsEvent / AuthenticationSuccessEvent オブジェクトからリスナー クラスのカスタム User オブジェクトを取得するにはどうすればよいですか?