Spring security 无法检测到SpringSecurity3.0 AuthenticationSuccessEvent

Spring security 无法检测到SpringSecurity3.0 AuthenticationSuccessEvent,spring-security,Spring Security,我想在用户登录系统后准备一些数据。在使用google之后,我实现了一个ApplicationListener来侦听AuthenticationSuccessEvent: import org.springframework.context.ApplicationListener; import org.springframework.security.authentication.event.AuthenticationSuccessEvent; import org.springframewo

我想在用户登录系统后准备一些数据。在使用google之后,我实现了一个ApplicationListener来侦听AuthenticationSuccessEvent:

import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

    @Component
    public class MyApplicationListener implements
            ApplicationListener<AuthenticationSuccessEvent> {

        @Override
        public void onApplicationEvent(AuthenticationSuccessEvent event) {
            UserDetails userDetails = (UserDetails) event.getAuthentication()
                    .getPrincipal();
            System.out.println("Successed login:" + userDetails.getUsername());

        }

    }
import org.springframework.context.ApplicationListener;
导入org.springframework.security.authentication.event.AuthenticationSuccessEvent;
导入org.springframework.security.core.userdetails.userdetails;
导入org.springframework.stereotype.Component;
@组成部分
公共类MyApplicationListener实现
应用程序侦听器{
@凌驾
ApplicationEvent上的公共无效(AuthenticationSuccessEvent事件){
UserDetails UserDetails=(UserDetails)事件。getAuthentication()
.getPrincipal();
System.out.println(“成功登录:+userDetails.getUsername());
}
}
我更新了Spring3.0版本和SpringSecurity 3.0.0.RC2。但我永远不会被调用AuthenticationSuccessEvent:((我尝试了其他事件,例如AuthenticationFailureBadCredentialsEvent,成功了)

我使用自己的身份验证管理器,不对事件执行任何操作:

我需要自己播客吗


谢谢。

我使用的是Spring Security 2.0.4,但我认为基本相同。 从我看到的情况来看,ProviderManager是在成功验证的情况下发布事件的

以下几个问题可能会有所帮助:

  • 您是使用标准ProviderManager(org.springframework.security.providers.ProviderManager)还是提供自己的ProviderManager
  • 也许@Component不起作用,也许(只是为了测试)您可以尝试使用常规的addListener()函数
  • 理解发生了什么的最好方法是调试Spring安全性(在ProviderManager中找到断点),我经常这样做,并发现它非常有用


    Shay

    我使用的是SpringSecurity2.0.4,但我认为基本相同。 从我看到的情况来看,ProviderManager是在成功验证的情况下发布事件的

    以下几个问题可能会有所帮助:

  • 您是使用标准ProviderManager(org.springframework.security.providers.ProviderManager)还是提供自己的ProviderManager
  • 也许@Component不起作用,也许(只是为了测试)您可以尝试使用常规的addListener()函数
  • 理解发生了什么的最好方法是调试Spring安全性(在ProviderManager中找到断点),我经常这样做,并发现它非常有用


    Shay

    您没有指定配置的详细信息。您声明您使用自己的身份验证管理器-这是否意味着您正在使用Spring Bean配置显式地配置
    ProviderManager

    如果是这样,您需要在
    ProviderManager
    上配置
    AuthenticationEventPublisher
    ,因为默认实现是空实现,它不发布事件

    默认实现的bean声明如下所示:

    <bean id="defaultAuthEventPublisher" class="org.springframework.security.authentication.DefaultAuthenticationEventPublisher"/>
    
    
    
    然后需要将此bean映射到
    ProviderManager
    上的相应属性:


    如果您没有声明自己的
    ProviderManager
    ,很遗憾,使用安全命名空间样式的配置无法启用此功能。希望这能回答您的问题!

    您没有指定配置的详细信息。您声明您使用自己的身份验证管理器-这是否意味着您是否使用Springbean配置显式配置
    ProviderManager

    如果是这样,您需要在
    ProviderManager
    上配置
    AuthenticationEventPublisher
    ,因为默认实现是空实现,它不发布事件

    默认实现的bean声明如下所示:

    <bean id="defaultAuthEventPublisher" class="org.springframework.security.authentication.DefaultAuthenticationEventPublisher"/>
    
    
    
    然后需要将此bean映射到
    ProviderManager
    上的相应属性:


    如果您没有声明自己的
    ProviderManager
    ,很遗憾,使用安全命名空间样式的配置无法启用此功能。希望这能回答您的问题!

    也许您想侦听InteractiveAuthenticationSuccessEvent。例如,OpenID仅发出该事件。另请参阅。

    可能您想侦听InteractiveAuthenticationSuccessEvent。例如,OpenID仅发出该事件。另请参见。

    我也有同样的问题,我发现了一些可能有用的方法

  • 我认为我们应该在web.xml中包含以下侦听器

    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    
    org.springframework.security.web.session.HttpSessionEventPublisher
    
  • 如果您想知道您的问题是在侦听器检测还是发布中,您可以自己启动事件

    SecurityContextHolder.getContext().getAuthentication(); AuthenticationSuccessEvent事件=新建AuthenticationSuccessEvent( SecurityContextHolder.getContext().getAuthentication()); eventPublisher.publishEvent(事件)


  • 我也有同样的问题,我发现了一些可能有用的东西

  • 我认为我们应该在web.xml中包含以下侦听器

    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    
    org.springframework.security.web.session.HttpSessionEventPublisher
    
  • 如果您想知道您的问题是在侦听器检测还是发布中,您可以自己启动事件

    SecurityContextHolder.getContext().getAuthentication(); AuthenticationSuccessEvent事件=新建AuthenticationSuccessEvent( SecurityContextHolder.getContext().getAuthentication()); eventPublisher.publishEvent(事件)