views:

95

answers:

1

Is it possible to create a custom @Aspect and apply it to the Classes/Methods within Spring Security (3.0.3)?

I'm trying to do some logging of logon/logoff requests and none of my Advices are being triggered.

I'm using @AspectJ annotations and here is how I'm decorating my method:

@After("execution (* org.springframework.security.authentication.ProviderManager.doAuthentication(..))")
+2  A: 

Rather use ApplicationListener to catch the successful login. See the Javadocs for other event types.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

@Component
public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {
    private static final Logger logger = LoggerFactory.getLogger(AuthenticationSuccessListener.class);

    @Override
    public void onApplicationEvent(final AuthenticationSuccessEvent event) {
        logger.debug("User {} logged in", event.getAuthentication().getName());
    }
}
hleinone
Good idea, thanks! I was, however, able to get my Aspect to work properly, but I'll also try this to get more familiar.
El Guapo