Spring security: adding "On successful login event listener"
You need to define a Spring Bean which implements ApplicationListener.
Then, in your code, do something like this:
public void onApplicationEvent(ApplicationEvent appEvent)
{
if (appEvent instanceof AuthenticationSuccessEvent)
{
AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) appEvent;
UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal();
// ....
}
}
Then, in your applicationContext.xml file, just define that bean and it will automatically start receiving events :)
The problem with AuthenticationSuccessEvent is it doesn't get published on remember-me login. If you're using remember-me authentication use InteractiveAuthenticationSuccessEvent instead, it works for normal login as well as for remember-me login.
@Component
public class LoginListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
@Override
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event)
{
UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal();
// ...
}
}