diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AbstractAuthenticationAuditListener.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AbstractAuthenticationAuditListener.java index b011058fab..5c0bf2a91a 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AbstractAuthenticationAuditListener.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AbstractAuthenticationAuditListener.java @@ -17,6 +17,8 @@ package org.springframework.boot.actuate.security; import org.springframework.boot.actuate.audit.AuditEvent; +import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationListener; import org.springframework.security.authentication.event.AbstractAuthenticationEvent; @@ -32,4 +34,21 @@ import org.springframework.security.authentication.event.AbstractAuthenticationE public abstract class AbstractAuthenticationAuditListener implements ApplicationListener, ApplicationEventPublisherAware { + private ApplicationEventPublisher publisher; + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { + this.publisher = publisher; + } + + protected ApplicationEventPublisher getPublisher() { + return this.publisher; + } + + protected void publish(AuditEvent event) { + if (getPublisher() != null) { + getPublisher().publishEvent(new AuditApplicationEvent(event)); + } + } + } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AbstractAuthorizationAuditListener.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AbstractAuthorizationAuditListener.java index 14057e4b5d..88e38c2cd7 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AbstractAuthorizationAuditListener.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AbstractAuthorizationAuditListener.java @@ -17,6 +17,8 @@ package org.springframework.boot.actuate.security; import org.springframework.boot.actuate.audit.AuditEvent; +import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationListener; import org.springframework.security.access.event.AbstractAuthorizationEvent; @@ -32,4 +34,21 @@ import org.springframework.security.access.event.AbstractAuthorizationEvent; public abstract class AbstractAuthorizationAuditListener implements ApplicationListener, ApplicationEventPublisherAware { + private ApplicationEventPublisher publisher; + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { + this.publisher = publisher; + } + + protected ApplicationEventPublisher getPublisher() { + return this.publisher; + } + + protected void publish(AuditEvent event) { + if (getPublisher() != null) { + getPublisher().publishEvent(new AuditApplicationEvent(event)); + } + } + } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthenticationAuditListener.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthenticationAuditListener.java index e5c481fcbb..87ff5f6fb0 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthenticationAuditListener.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthenticationAuditListener.java @@ -20,8 +20,6 @@ import java.util.HashMap; import java.util.Map; import org.springframework.boot.actuate.audit.AuditEvent; -import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent; -import org.springframework.context.ApplicationEventPublisher; import org.springframework.security.authentication.event.AbstractAuthenticationEvent; import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent; import org.springframework.security.authentication.event.AuthenticationSuccessEvent; @@ -37,15 +35,8 @@ public class AuthenticationAuditListener extends AbstractAuthenticationAuditList private static final String WEB_LISTENER_CHECK_CLASS = "org.springframework.security.web.authentication.switchuser.AuthenticationSwitchUserEvent"; - private ApplicationEventPublisher publisher; - private WebAuditListener webListener = maybeCreateWebListener(); - @Override - public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { - this.publisher = publisher; - } - private static WebAuditListener maybeCreateWebListener() { if (ClassUtils.isPresent(WEB_LISTENER_CHECK_CLASS, null)) { return new WebAuditListener(); @@ -83,12 +74,6 @@ public class AuthenticationAuditListener extends AbstractAuthenticationAuditList "AUTHENTICATION_SUCCESS", data)); } - private void publish(AuditEvent event) { - if (this.publisher != null) { - this.publisher.publishEvent(new AuditApplicationEvent(event)); - } - } - private static class WebAuditListener { public void process(AuthenticationAuditListener listener, diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthorizationAuditListener.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthorizationAuditListener.java index 2d672e0294..a71d1c49b7 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthorizationAuditListener.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/security/AuthorizationAuditListener.java @@ -20,26 +20,17 @@ import java.util.HashMap; import java.util.Map; import org.springframework.boot.actuate.audit.AuditEvent; -import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent; -import org.springframework.context.ApplicationEventPublisher; import org.springframework.security.access.event.AbstractAuthorizationEvent; import org.springframework.security.access.event.AuthenticationCredentialsNotFoundEvent; import org.springframework.security.access.event.AuthorizationFailureEvent; /** - * Default implementation of {@link AuthorizationAuditListener}. + * Default implementation of {@link AbstractAuthorizationAuditListener}. * * @author Dave Syer */ public class AuthorizationAuditListener extends AbstractAuthorizationAuditListener { - private ApplicationEventPublisher publisher; - - @Override - public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { - this.publisher = publisher; - } - @Override public void onApplicationEvent(AbstractAuthorizationEvent event) { if (event instanceof AuthenticationCredentialsNotFoundEvent) { @@ -67,10 +58,4 @@ public class AuthorizationAuditListener extends AbstractAuthorizationAuditListen "AUTHORIZATION_FAILURE", data)); } - private void publish(AuditEvent event) { - if (this.publisher != null) { - this.publisher.publishEvent(new AuditApplicationEvent(event)); - } - } - }