41

I'm new to Spring Security. How do I add an event listener which will be called as a user logs in successfully? Also I need to get some kind of unique session ID in this listener which should be available further on. I need this ID to synchronize with another server.

5 Answers 5

55

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 :)

3
  • Thanks! Just found AuthenticationSuccessEvent but was trying to figure out how to register a listener.
    – axk
    Oct 8, 2008 at 12:36
  • 2
    what about the session ID he was asking about?
    – siebmanb
    Feb 26, 2015 at 10:28
  • 1
    @siebmanb just add @Autowired HttpSession session to the listener. Spring will inject a proxy which automagically delegates to the correction session. Feb 17, 2016 at 22:43
51

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();
        // ...
    }
}
1
26

Similar to Phill's answer, but modified to take Generics into consideration:

public class AuthenticationListener implements ApplicationListener<AuthenticationSuccessEvent> {

  @Override
  public void onApplicationEvent(final AuthenticationSuccessEvent event) {

      // ...

  }

}
11

Another way using @EventListener

@EventListener
public void doSomething(InteractiveAuthenticationSuccessEvent event) { // any spring event
    // your code 

}
1
  • This one is really simple! Like! Nov 21, 2020 at 12:52
10

In Grails, with Spring Security Plugin, you can do this in Config.groovy:

grails.plugins.springsecurity.useSecurityEventListener = true

grails.plugins.springsecurity.onAuthenticationSuccessEvent = { e, appCtx ->

        def session = SecurityRequestHolder.request.getSession(false)
        session.myVar = true

}
2
  • Right way to get request in Config.groovy def request= RequestContextHolder?.currentRequestAttributes();
    – openSource
    Apr 25, 2013 at 23:37
  • 1
    Does anyone know how to get the current logged in user in this event? I am currently using def springSecurityService = Holders.grailsApplication.mainContext.getBean 'springSecurityService'; def user = springSecurityService.getPrincipal() but user is always null. Thank you! EDIT: Looks like doing def user = event.getAuthentication().getPrincipal() works great!
    – Pudpuduk
    Jun 10, 2014 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.