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
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 :)
-
Thanks! Just found AuthenticationSuccessEvent but was trying to figure out how to register a listener.– axkOct 8, 2008 at 12:36
-
2
-
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
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();
// ...
}
}
-
1Thanks a lot! I've tried to achieve this by using auth-success-handler, but faced with this problem: stackoverflow.com/questions/11575860/…. Your answer rescued situation!– ynameOct 22, 2013 at 0:57
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) {
// ...
}
}
Another way using @EventListener
@EventListener
public void doSomething(InteractiveAuthenticationSuccessEvent event) { // any spring event
// your code
}
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
}
-
Right way to get request in Config.groovy def request= RequestContextHolder?.currentRequestAttributes(); Apr 25, 2013 at 23:37
-
1Does 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!– PudpudukJun 10, 2014 at 6:57