Spring Security和AOP

时间:2010-08-09 01:26:49

标签: spring spring-security aop spring-aop

是否可以创建自定义@Aspect并将其应用于Spring Security(3.0.3)中的类/方法?

我正在尝试记录一些登录/注销请求,但我的建议都没有被触发。

我正在使用@AspectJ注释,这就是我如何装饰我的方法:

@After("execution (* org.springframework.security.authentication.ProviderManager.doAuthentication(..))")

1 个答案:

答案 0 :(得分:2)

而是使用ApplicationListener来捕获成功登录。有关其他事件类型,请参阅Javadocs

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