在Spring Security中没有捕获AuthenticationSuccessEvent

时间:2015-11-27 09:12:31

标签: java spring spring-mvc spring-security

您已在用户登录或注销时为侦听身份验证事件创建了自定义ApplicationEvent侦听器。但是我的听众只能抓住ServletRequestHandledEvent。我无法弄清楚问题。

这是我的听众课程

    @Component
public class MyApplicationEventListener implements ApplicationListener<ApplicationEvent>{
    @Override
    public void onApplicationEvent(final ApplicationEvent appEvent) {
        System.out.println(appEvent);
        if (appEvent instanceof AuthenticationSuccessEvent) {
            final AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) appEvent;
            final String sessionId =  ((WebAuthenticationDetails) event.getAuthentication().getDetails()).getSessionId();
            RACache.MODULE_QUERY_MAP.put(sessionId, new HashMap<String, Set<String>>());
        } else if (appEvent instanceof SessionDestroyedEvent) {
            final SessionDestroyedEvent event = (SessionDestroyedEvent) appEvent;
            final String sessionId =  event.getId();
            RACache.MODULE_QUERY_MAP.remove(sessionId);
        }if (appEvent instanceof InteractiveAuthenticationSuccessEvent) {
            final AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) appEvent;
            final String sessionId =  ((WebAuthenticationDetails) event.getAuthentication().getDetails()).getSessionId();
            RACache.MODULE_QUERY_MAP.put(sessionId, new HashMap<String, Set<String>>());
        }
    }
}

以下是我的spring config xml文件。

<http use-expressions="true" authentication-manager-ref="authenticationManager">
....
</http>

    <beans:bean id="authenticationManager"
        class="org.springframework.security.authentication.ProviderManager">
        <beans:property name="authenticationEventPublisher" ref="defaultAuthEventPublisher"/>
        <beans:property name="providers">
            <beans:list>
                <beans:ref bean="authenticationProvider" />
                <beans:ref bean="anonymousProvider" />
            </beans:list>
        </beans:property>
    </beans:bean>


    <beans:bean id="anonymousProvider" class="org.springframework.security.authentication.AnonymousAuthenticationProvider">
        <beans:property name="key" value="RAC" />
    </beans:bean>

    <beans:bean id="authenticationProvider"
        class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="passwordEncoder" ref="encoder" />
        <beans:property name="userDetailsService" ref="userService" />
    </beans:bean>

    <beans:bean id="encoder"  class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
  </beans:bean>

<jdbc-user-service id="userService"
        data-source-ref="dataSource"
        users-by-username-query="select username,password,enabled from userinfo where username=?;"
        authorities-by-username-query="select username, role_name from user_roles where username =?;" />

我还在web.xml中添加了以下内容

    <listener>
    <listener-class>
        org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

0 个答案:

没有答案