Skip to content

Commit 0286d36

Browse files
committed
Add authentication event Kotlin samples
Issue gh-8172
1 parent 6695874 commit 0286d36

File tree

1 file changed

+69
-4
lines changed
  • docs/manual/src/docs/asciidoc/_includes/servlet/authentication

1 file changed

+69
-4
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/events.adoc

+69-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ For each authentication that succeeds or fails, a `AuthenticationSuccessEvent` o
66
To listen for these events, you must first publish an `AuthenticationEventPublisher`.
77
Spring Security's `DefaultAuthenticationEventPublisher` will probably do fine:
88

9-
[source,java]
9+
====
10+
.Java
11+
[source,java,role="primary"]
1012
----
1113
@Bean
1214
public AuthenticationEventPublisher authenticationEventPublisher
@@ -15,9 +17,22 @@ public AuthenticationEventPublisher authenticationEventPublisher
1517
}
1618
----
1719
20+
.Kotlin
21+
[source,kotlin,role="secondary"]
22+
----
23+
@Bean
24+
fun authenticationEventPublisher
25+
(applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {
26+
return DefaultAuthenticationEventPublisher(applicationEventPublisher)
27+
}
28+
----
29+
====
30+
1831
Then, you can use Spring's `@EventListener` support:
1932

20-
[source,java]
33+
====
34+
.Java
35+
[source,java,role="primary"]
2136
----
2237
@Component
2338
public class AuthenticationEvents {
@@ -33,6 +48,24 @@ public class AuthenticationEvents {
3348
}
3449
----
3550
51+
.Kotlin
52+
[source,kotlin,role="secondary"]
53+
----
54+
@Component
55+
class AuthenticationEvents {
56+
@EventListener
57+
fun onSuccess(success: AuthenticationSuccessEvent?) {
58+
// ...
59+
}
60+
61+
@EventListener
62+
fun onFailure(failures: AbstractAuthenticationFailureEvent?) {
63+
// ...
64+
}
65+
}
66+
----
67+
====
68+
3669
While similar to `AuthenticationSuccessHandler` and `AuthenticationFailureHandler`, these are nice in that they can be used independently from the servlet API.
3770

3871
=== Adding Exception Mappings
@@ -56,7 +89,9 @@ The publisher does an exact `Exception` match, which means that sub-classes of t
5689

5790
To that end, you may want to supply additional mappings to the publisher via the `setAdditionalExceptionMappings` method:
5891

59-
[source,java]
92+
====
93+
.Java
94+
[source,java,role="primary"]
6095
----
6196
@Bean
6297
public AuthenticationEventPublisher authenticationEventPublisher
@@ -71,11 +106,28 @@ public AuthenticationEventPublisher authenticationEventPublisher
71106
}
72107
----
73108
109+
.Kotlin
110+
[source,kotlin,role="secondary"]
111+
----
112+
@Bean
113+
fun authenticationEventPublisher
114+
(applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {
115+
val mapping: Map<Class<out AuthenticationException>, Class<out AbstractAuthenticationFailureEvent>> =
116+
mapOf(Pair(FooException::class.java, FooEvent::class.java))
117+
val authenticationEventPublisher = DefaultAuthenticationEventPublisher(applicationEventPublisher)
118+
authenticationEventPublisher.setAdditionalExceptionMappings(mapping)
119+
return authenticationEventPublisher
120+
}
121+
----
122+
====
123+
74124
=== Default Event
75125

76126
And, you can supply a catch-all event to fire in the case of any `AuthenticationException`:
77127

78-
[source,java]
128+
====
129+
.Java
130+
[source,java,role="primary"]
79131
----
80132
@Bean
81133
public AuthenticationEventPublisher authenticationEventPublisher
@@ -87,3 +139,16 @@ public AuthenticationEventPublisher authenticationEventPublisher
87139
return authenticationEventPublisher;
88140
}
89141
----
142+
143+
.Kotlin
144+
[source,kotlin,role="secondary"]
145+
----
146+
@Bean
147+
fun authenticationEventPublisher
148+
(applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {
149+
val authenticationEventPublisher = DefaultAuthenticationEventPublisher(applicationEventPublisher)
150+
authenticationEventPublisher.setDefaultAuthenticationFailureEvent(GenericAuthenticationFailureEvent::class.java)
151+
return authenticationEventPublisher
152+
}
153+
----
154+
====

0 commit comments

Comments
 (0)