title: How to get custom User object from AuthenticationFailureBadCredentialsEvent / AuthenticationSuccessEvent object
body: <p>I'm trying to show no of invalid attempts of user using spring security.I'm using a custom User class to get additional user details other than username and password. I've created two listener classes i.e. AuthenticationSuccessEventListener &amp; AuthenticationFailureListener to update user's invalid attempts.</p>
  
  <p>Now in the onApplicationEvent method i'm trying to get custom User object (CustomUserDetails) like shown below:</p>
  
  <pre><code>    @Component
      public class AuthenticationFailureListener implements ApplicationListener&lt;AuthenticationFailureBadCredentialsEvent&gt; {
          @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){
          ...
         } } }
  </code></pre>
  
  <p>event.getAuthentication().getPrincipal() returns a String i.e. username which i'm trying to cast it to CustomUserDetails (custom User class) and i get error.</p>
  
  <p>P.S - I'm entering userid/password in login page and hence i pass userid as parameter for all the methods including loadUserByUsername(strUserID).</p>
  
  <p>How can i get my custom User object in the listener class from AuthenticationFailureBadCredentialsEvent / AuthenticationSuccessEvent object?</p>