Spring安全与AOP

发布于 2024-09-13 19:28:03 字数 269 浏览 4 评论 0原文

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

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

我正在使用 @AspectJ 注释,以下是我装饰方法的方式:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-09-20 19:28:03

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

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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文