【问题标题】:Spring Security and AOPSpring Security 和 AOP
【发布时间】:2010-08-09 01:26:48
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: spring spring-security aop spring-aop


    【解决方案1】:

    而是使用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());
        }
    }
    

    【讨论】:

    • 好主意,谢谢!但是,我能够让我的 Aspect 正常工作,但我也会尝试这样做以更加熟悉。
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    相关资源
    最近更新 更多