How to capture the authentication events?

spring-security

Solution

If all you want to do is get notified about authentication events, then you don't have to customize the `AuthenticationManager`. It's enough to implement the `ApplicationListener` interface as shown below, and put a bean of this type in the same spring context where security is configured.

public class AuthenticationEventListener 
        implements ApplicationListener<AbstractAuthenticationEvent> {

    @Override
    public void onApplicationEvent(AbstractAuthenticationEvent event) {
        // process the event
    }
}

Problem

I tried the following: ``` <bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager"> <property name="providers"> <list> <ref local="myAuthnProvider"/> </list> </property> <property name="authenticationEventPublisher"> <bean class="myPublisher/> </property> </bean> <security:authentication-manager> <security:authentication-provider ref="authenticationManager" /> </security:authentication-manager> ``` But I get an exception during startup. I also tried with the constructor tag, same result: ``` Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Could not resolve matching constructor (hint: secify index/type/name arguments for simple parameters to avoid type ambiguities) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:250) ``` I'm using spring 3.1.1. Any ideas why this error is thrown? Or other suggestions on how to capture the authentication events? Thanks.

Original source