From 0286d368c354aa0286c2ad1927ea44c150180498 Mon Sep 17 00:00:00 2001 From: Eleftheria Stein Date: Mon, 28 Sep 2020 17:19:38 +0200 Subject: [PATCH] Add authentication event Kotlin samples Issue gh-8172 --- .../servlet/authentication/events.adoc | 73 ++++++++++++++++++- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/docs/manual/src/docs/asciidoc/_includes/servlet/authentication/events.adoc b/docs/manual/src/docs/asciidoc/_includes/servlet/authentication/events.adoc index 2448fdff6d..254df9a669 100644 --- a/docs/manual/src/docs/asciidoc/_includes/servlet/authentication/events.adoc +++ b/docs/manual/src/docs/asciidoc/_includes/servlet/authentication/events.adoc @@ -6,7 +6,9 @@ For each authentication that succeeds or fails, a `AuthenticationSuccessEvent` o To listen for these events, you must first publish an `AuthenticationEventPublisher`. Spring Security's `DefaultAuthenticationEventPublisher` will probably do fine: -[source,java] +==== +.Java +[source,java,role="primary"] ---- @Bean public AuthenticationEventPublisher authenticationEventPublisher @@ -15,9 +17,22 @@ public AuthenticationEventPublisher authenticationEventPublisher } ---- +.Kotlin +[source,kotlin,role="secondary"] +---- +@Bean +fun authenticationEventPublisher + (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher { + return DefaultAuthenticationEventPublisher(applicationEventPublisher) +} +---- +==== + Then, you can use Spring's `@EventListener` support: -[source,java] +==== +.Java +[source,java,role="primary"] ---- @Component public class AuthenticationEvents { @@ -33,6 +48,24 @@ public class AuthenticationEvents { } ---- +.Kotlin +[source,kotlin,role="secondary"] +---- +@Component +class AuthenticationEvents { + @EventListener + fun onSuccess(success: AuthenticationSuccessEvent?) { + // ... + } + + @EventListener + fun onFailure(failures: AbstractAuthenticationFailureEvent?) { + // ... + } +} +---- +==== + While similar to `AuthenticationSuccessHandler` and `AuthenticationFailureHandler`, these are nice in that they can be used independently from the servlet API. === Adding Exception Mappings @@ -56,7 +89,9 @@ The publisher does an exact `Exception` match, which means that sub-classes of t To that end, you may want to supply additional mappings to the publisher via the `setAdditionalExceptionMappings` method: -[source,java] +==== +.Java +[source,java,role="primary"] ---- @Bean public AuthenticationEventPublisher authenticationEventPublisher @@ -71,11 +106,28 @@ public AuthenticationEventPublisher authenticationEventPublisher } ---- +.Kotlin +[source,kotlin,role="secondary"] +---- +@Bean +fun authenticationEventPublisher + (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher { + val mapping: Map, Class> = + mapOf(Pair(FooException::class.java, FooEvent::class.java)) + val authenticationEventPublisher = DefaultAuthenticationEventPublisher(applicationEventPublisher) + authenticationEventPublisher.setAdditionalExceptionMappings(mapping) + return authenticationEventPublisher +} +---- +==== + === Default Event And, you can supply a catch-all event to fire in the case of any `AuthenticationException`: -[source,java] +==== +.Java +[source,java,role="primary"] ---- @Bean public AuthenticationEventPublisher authenticationEventPublisher @@ -87,3 +139,16 @@ public AuthenticationEventPublisher authenticationEventPublisher return authenticationEventPublisher; } ---- + +.Kotlin +[source,kotlin,role="secondary"] +---- +@Bean +fun authenticationEventPublisher + (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher { + val authenticationEventPublisher = DefaultAuthenticationEventPublisher(applicationEventPublisher) + authenticationEventPublisher.setDefaultAuthenticationFailureEvent(GenericAuthenticationFailureEvent::class.java) + return authenticationEventPublisher +} +---- +====