- 
                Notifications
    You must be signed in to change notification settings 
- Fork 41.6k
Publish an AuditEvent on logout #41278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -24,6 +24,7 @@ | |
| import org.springframework.security.authentication.event.AbstractAuthenticationEvent; | ||
| import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent; | ||
| import org.springframework.security.authentication.event.AuthenticationSuccessEvent; | ||
| import org.springframework.security.authentication.event.LogoutSuccessEvent; | ||
| import org.springframework.security.web.authentication.switchuser.AuthenticationSwitchUserEvent; | ||
| import org.springframework.util.ClassUtils; | ||
|  | ||
|  | @@ -51,6 +52,8 @@ public class AuthenticationAuditListener extends AbstractAuthenticationAuditList | |
| */ | ||
| public static final String AUTHENTICATION_SWITCH = "AUTHENTICATION_SWITCH"; | ||
|  | ||
| public static final String LOGOUT_SUCCESS = "LOGOUT_SUCCESS"; | ||
|  | ||
| private static final String WEB_LISTENER_CHECK_CLASS = "org.springframework.security.web.authentication.switchuser.AuthenticationSwitchUserEvent"; | ||
|  | ||
| private final WebAuditListener webListener = maybeCreateWebListener(); | ||
|  | @@ -73,6 +76,9 @@ else if (this.webListener != null && this.webListener.accepts(event)) { | |
| else if (event instanceof AuthenticationSuccessEvent successEvent) { | ||
| onAuthenticationSuccessEvent(successEvent); | ||
| } | ||
| else if(event instanceof LogoutSuccessEvent logoutSuccessEvent) { | ||
|          | ||
| onLogoutSuccessEvent(logoutSuccessEvent); | ||
| } | ||
| } | ||
|  | ||
| private void onAuthenticationFailureEvent(AbstractAuthenticationFailureEvent event) { | ||
|  | @@ -93,6 +99,18 @@ private void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) { | |
| publish(new AuditEvent(event.getAuthentication().getName(), AUTHENTICATION_SUCCESS, data)); | ||
| } | ||
|  | ||
| private void onLogoutSuccessEvent(LogoutSuccessEvent event) { | ||
| Map<String, Object> data = new LinkedHashMap<>(); | ||
| if(event.getAuthentication() != null) { | ||
|          | ||
| if(event.getAuthentication().getDetails() != null) { | ||
| data.put("details", event.getAuthentication().getDetails()); | ||
| } | ||
| data.put("username", event.getAuthentication().getName()); | ||
|          | ||
| } | ||
| publish(new AuditEvent(event.getAuthentication().getName(), LOGOUT_SUCCESS, data)); | ||
|  | ||
| } | ||
|  | ||
| private static final class WebAuditListener { | ||
|  | ||
| void process(AuthenticationAuditListener listener, AbstractAuthenticationEvent input) { | ||
|  | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some javadoc here that describes the constant. It should also be marked with
@since 3.4.0.