El Guapo
El Guapo

Reputation: 5781

Spring Security and AOP

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(..))")

Upvotes: 1

Views: 2298

Answers (1)

hleinone
hleinone

Reputation: 4480

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());
    }
}

Upvotes: 2

Related Questions