【问题标题】:Application Listener not receiving event notification应用程序侦听器未收到事件通知
【发布时间】:2011-05-20 14:53:21
【问题描述】:

我正在开发一个使用 Spring Security 进行验证的 Spring MVC 应用程序。我想在登录时做一些工作,并在多个地方(包括enter link description here)看到了使用应用程序侦听器来实现此目的的建议。我已经开始实施它:

@Named
public class AccountLoginListener implements ApplicationListener<AuthenticationSuccessEvent> {

    @Inject
    AccountService accountService;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        Account account = (Account) event.getAuthentication().getDetails();
        ...
        accountService.saveAccount(account);
    }

}

不幸的是,AuthenticationSuccessEvent 似乎没有被捕获,并且当我调试时,永远不会调用 onApplicationEvent 函数。我没有在xml文件中做任何额外的配置,但我认为没有必要这样做。我错过了一些配置,还是我做错了什么?

谢谢!

爱宾利

【问题讨论】:

  • 你是如何创建实例化这个 bean/listener 的?
  • 我的spring上下文有一个&lt;context:component-scan&gt;,这个类在它的基础包中。
  • @Named注解是什么类型的?
  • 这是一个 jsr330 注释,功能上等同于(我相信)@Component

标签: java spring spring-mvc spring-security listener


【解决方案1】:

如果你使用的是 Spring Security 3,你应该实现 AuthenticationSuccessHandler,然后在你的应用上下文中配置这个 bean。

【讨论】:

  • 感谢这位大卫 - 我会看看有什么区别。我的问题最终是因为我们应用程序中的 web 上下文与 spring 上下文不同。
【解决方案2】:
public class AuthenticationSuccessListener
    implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {

    @Inject
    AccountService accountService;

    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
        Account account = (Account) event.getAuthentication().getDetails();
        accountService.saveAccount(account);
    }
}

【讨论】:

    猜你喜欢
    • 2010-11-21
    • 2018-02-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2022-01-16
    • 2019-03-10
    相关资源
    最近更新 更多