Friday, April 24, 2009

Spring Security AuthenticationSuccessListener

Ever asked how to do things on a successful authentication when using Spring Security?

It's very easy. You just need a ApplicationListener that listens for an InteractiveAuthenticationSuccessEvent.

First create a AuthenticationSuccessListener

package example.web.security;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.security.event.
         authentication.
         InteractiveAuthenticationSuccessEvent;

  public class AuthenticationSuccessListener 
    implements ApplicationListener {

  public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof InteractiveAuthenticationSuccessEvent) {

      // do things on authentication ...
    }
  }
}

Then declare a bean somewhere in Spring Application Context

...
<bean class="example.web.security.AuthenticationSuccessListener" />
...
Youre done!

No comments:

Post a Comment