# 认证事件

对于每个成功或失败的身份验证，将分别触发`AuthenticationSuccessEvent`或`AbstractAuthenticationFailureEvent`。

要侦听这些事件，你必须首先发布`AuthenticationEventPublisher`。 Spring 安全性的`DefaultAuthenticationEventPublisher`可能做得很好：

爪哇

```
@Bean
public AuthenticationEventPublisher authenticationEventPublisher
        (ApplicationEventPublisher applicationEventPublisher) {
    return new DefaultAuthenticationEventPublisher(applicationEventPublisher);
}
```

Kotlin

```
@Bean
fun authenticationEventPublisher
        (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {
    return DefaultAuthenticationEventPublisher(applicationEventPublisher)
}
```

然后，你可以使用 Spring 的`@EventListener`支持：

爪哇

```
@Component
public class AuthenticationEvents {
	@EventListener
    public void onSuccess(AuthenticationSuccessEvent success) {
		// ...
    }

    @EventListener
    public void onFailure(AbstractAuthenticationFailureEvent failures) {
		// ...
    }
}
```

Kotlin

```
@Component
class AuthenticationEvents {
    @EventListener
    fun onSuccess(success: AuthenticationSuccessEvent?) {
        // ...
    }

    @EventListener
    fun onFailure(failures: AbstractAuthenticationFailureEvent?) {
        // ...
    }
}
```

虽然类似于`AuthenticationSuccessHandler`和`AuthenticationFailureHandler`，但它们很好，因为它们可以独立于 Servlet API 使用。

## 添加异常映射

默认情况下，`DefaultAuthenticationEventPublisher`将为以下事件发布`AbstractAuthenticationFailureEvent`:

|           Exception            |事件|
|--------------------------------|----------------------------------------------|
|   `BadCredentialsException`    |`AuthenticationFailureBadCredentialsEvent`|
|  `UsernameNotFoundException`   |`AuthenticationFailureBadCredentialsEvent`|
|   `AccountExpiredException`    |`AuthenticationFailureExpiredEvent`|
|  `ProviderNotFoundException`   |`AuthenticationFailureProviderNotFoundEvent`|
|      `DisabledException`       |`AuthenticationFailureDisabledEvent`|
|       `LockedException`        |`AuthenticationFailureLockedEvent`|
|`AuthenticationServiceException`|`AuthenticationFailureServiceExceptionEvent`|
| `CredentialsExpiredException`  |`AuthenticationFailureCredentialsExpiredEvent`|
| `InvalidBearerTokenException`  |`AuthenticationFailureBadCredentialsEvent`|

发布者进行完全的`Exception`匹配，这意味着这些异常的子类也不会产生事件。

为此，你可能希望通过`setAdditionalExceptionMappings`方法向发布服务器提供额外的映射：

爪哇

```
@Bean
public AuthenticationEventPublisher authenticationEventPublisher
        (ApplicationEventPublisher applicationEventPublisher) {
    Map<Class<? extends AuthenticationException>,
        Class<? extends AbstractAuthenticationFailureEvent>> mapping =
            Collections.singletonMap(FooException.class, FooEvent.class);
    AuthenticationEventPublisher authenticationEventPublisher =
        new DefaultAuthenticationEventPublisher(applicationEventPublisher);
    authenticationEventPublisher.setAdditionalExceptionMappings(mapping);
    return authenticationEventPublisher;
}
```

Kotlin

```
@Bean
fun authenticationEventPublisher
        (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {
    val mapping: Map<Class<out AuthenticationException>, Class<out AbstractAuthenticationFailureEvent>> =
            mapOf(Pair(FooException::class.java, FooEvent::class.java))
    val authenticationEventPublisher = DefaultAuthenticationEventPublisher(applicationEventPublisher)
    authenticationEventPublisher.setAdditionalExceptionMappings(mapping)
    return authenticationEventPublisher
}
```

## 默认事件

并且，你可以在任何`AuthenticationException`的情况下提供一个包罗万象的事件：

爪哇

```
@Bean
public AuthenticationEventPublisher authenticationEventPublisher
        (ApplicationEventPublisher applicationEventPublisher) {
    AuthenticationEventPublisher authenticationEventPublisher =
        new DefaultAuthenticationEventPublisher(applicationEventPublisher);
    authenticationEventPublisher.setDefaultAuthenticationFailureEvent
        (GenericAuthenticationFailureEvent.class);
    return authenticationEventPublisher;
}
```

Kotlin

```
@Bean
fun authenticationEventPublisher
        (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {
    val authenticationEventPublisher = DefaultAuthenticationEventPublisher(applicationEventPublisher)
    authenticationEventPublisher.setDefaultAuthenticationFailureEvent(GenericAuthenticationFailureEvent::class.java)
    return authenticationEventPublisher
}
```

[Logout](logout.html)[授权](../authorization/index.html)

