title: Capture Authentication failure in Spring Security
body: <p>I've got below class to Authenticate users in a REST Service</p>
  
  <pre><code>@Service
  public class AuthenticationService {
  
      private static final Logger log = LoggerFactory.getLogger(AuthenticationService.class);
  
      @Autowired
      private AuthenticationManager authenticationManager;
  
      public void getAuthentication(AccountCredentials credentials) {
  
          log.info("Authentication requested for user: " + credentials.getEmail());
  
          final Authentication authentication = authenticationManager.authenticate(
                  new UsernamePasswordAuthenticationToken(
                          credentials.getEmail(),
                          credentials.getPassword()
                  )
          );
  
          SecurityContextHolder.getContext().setAuthentication(authentication);
      }
  }
  </code></pre>
  
  <p>The Authentication works fine but I would like to log every time there is a Authentication failure. The way it works now is that a <code>403</code> status is returned by default by Spring Security. How can I capture Authentication failures in a log? I've tried different ways but the code is never reached since after the <code>403</code> is returned there is no other code executed. I've also tried with a <code>try catch</code> block but that messes up the flow and I would have to code the <code>403</code> response. I like to keep the <code>403</code> status message and that no other code is run besides that it is captured in a log.</p>