Skip to content

Commit c5953fe

Browse files
Chu3laManmhalbritter
authored andcommitted
Publish an AuditEvent on logout
See gh-41278
1 parent 5689bf5 commit c5953fe

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthenticationAuditListener.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
2525
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
2626
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
27+
import org.springframework.security.authentication.event.LogoutSuccessEvent;
2728
import org.springframework.security.web.authentication.switchuser.AuthenticationSwitchUserEvent;
2829
import org.springframework.util.ClassUtils;
2930

@@ -51,6 +52,14 @@ public class AuthenticationAuditListener extends AbstractAuthenticationAuditList
5152
*/
5253
public static final String AUTHENTICATION_SWITCH = "AUTHENTICATION_SWITCH";
5354

55+
/**
56+
* This constant is used to indicate that the logout process
57+
* has been completed successfully.
58+
*
59+
* @since 3.4.0
60+
*/
61+
public static final String LOGOUT_SUCCESS = "LOGOUT_SUCCESS";
62+
5463
private static final String WEB_LISTENER_CHECK_CLASS = "org.springframework.security.web.authentication.switchuser.AuthenticationSwitchUserEvent";
5564

5665
private final WebAuditListener webListener = maybeCreateWebListener();
@@ -73,6 +82,9 @@ else if (this.webListener != null && this.webListener.accepts(event)) {
7382
else if (event instanceof AuthenticationSuccessEvent successEvent) {
7483
onAuthenticationSuccessEvent(successEvent);
7584
}
85+
else if (event instanceof LogoutSuccessEvent logoutSuccessEvent) {
86+
onLogoutSuccessEvent(logoutSuccessEvent);
87+
}
7688
}
7789

7890
private void onAuthenticationFailureEvent(AbstractAuthenticationFailureEvent event) {
@@ -93,6 +105,15 @@ private void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
93105
publish(new AuditEvent(event.getAuthentication().getName(), AUTHENTICATION_SUCCESS, data));
94106
}
95107

108+
private void onLogoutSuccessEvent(LogoutSuccessEvent event) {
109+
Map<String, Object> data = new LinkedHashMap<>();
110+
if (event.getAuthentication().getDetails() != null) {
111+
data.put("details", event.getAuthentication().getDetails());
112+
}
113+
publish(new AuditEvent(event.getAuthentication().getName(), LOGOUT_SUCCESS, data));
114+
115+
}
116+
96117
private static final class WebAuditListener {
97118

98119
void process(AuthenticationAuditListener listener, AbstractAuthenticationEvent input) {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/security/AuthenticationAuditListenerTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.security.authentication.event.AuthenticationFailureExpiredEvent;
3030
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
3131
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
32+
import org.springframework.security.authentication.event.LogoutSuccessEvent;
3233
import org.springframework.security.core.authority.AuthorityUtils;
3334
import org.springframework.security.core.userdetails.User;
3435
import org.springframework.security.web.authentication.switchuser.AuthenticationSwitchUserEvent;
@@ -60,6 +61,13 @@ void testAuthenticationSuccess() {
6061
assertThat(event.getAuditEvent().getType()).isEqualTo(AuthenticationAuditListener.AUTHENTICATION_SUCCESS);
6162
}
6263

64+
@Test
65+
void testLogoutSucess() {
66+
AuditApplicationEvent event = handleAuthenticationEvent(
67+
new LogoutSuccessEvent(new UsernamePasswordAuthenticationToken("user", "password")));
68+
assertThat(event.getAuditEvent().getType()).isEqualTo(AuthenticationAuditListener.LOGOUT_SUCCESS);
69+
}
70+
6371
@Test
6472
void testOtherAuthenticationSuccess() {
6573
this.listener.onApplicationEvent(new InteractiveAuthenticationSuccessEvent(

0 commit comments

Comments
 (0)