Commit 0b70710b authored by Vedran Pavic's avatar Vedran Pavic Committed by Phillip Webb

Allow customization of AuditListener

Introduce `AbstractAuditListener` so that users can extended it to
replace the auto-configured default.

Fixes gh-5830
parent 6c21ba11
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.audit.listener;
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.context.ApplicationListener;
/**
* Abstract {@link ApplicationListener} to handle {@link AuditApplicationEvent}s.
*
* @author Vedran Pavic
* @since 1.4.0
*/
public abstract class AbstractAuditListener
implements ApplicationListener<AuditApplicationEvent> {
@Override
public void onApplicationEvent(AuditApplicationEvent event) {
onAuditEvent(event.getAuditEvent());
}
protected abstract void onAuditEvent(AuditEvent event);
}
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,17 +19,18 @@ package org.springframework.boot.actuate.audit.listener; ...@@ -19,17 +19,18 @@ package org.springframework.boot.actuate.audit.listener;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.audit.AuditEventRepository; import org.springframework.boot.actuate.audit.AuditEventRepository;
import org.springframework.context.ApplicationListener;
/** /**
* {@link ApplicationListener} that listens for {@link AuditApplicationEvent}s and stores * The default {@link AbstractAuditListener} implementation. Listens for
* them in a {@link AuditEventRepository}. * {@link AuditApplicationEvent}s and stores them in a {@link AuditEventRepository}.
* *
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Vedran Pavic
*/ */
public class AuditListener implements ApplicationListener<AuditApplicationEvent> { public class AuditListener extends AbstractAuditListener {
private static final Log logger = LogFactory.getLog(AuditListener.class); private static final Log logger = LogFactory.getLog(AuditListener.class);
...@@ -40,11 +41,11 @@ public class AuditListener implements ApplicationListener<AuditApplicationEvent> ...@@ -40,11 +41,11 @@ public class AuditListener implements ApplicationListener<AuditApplicationEvent>
} }
@Override @Override
public void onApplicationEvent(AuditApplicationEvent event) { protected void onAuditEvent(AuditEvent event) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(event.getAuditEvent()); logger.debug(event);
} }
this.auditEventRepository.add(event.getAuditEvent()); this.auditEventRepository.add(event);
} }
} }
...@@ -20,6 +20,7 @@ import org.springframework.beans.factory.ObjectProvider; ...@@ -20,6 +20,7 @@ import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.audit.AuditEvent; import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.audit.AuditEventRepository; import org.springframework.boot.actuate.audit.AuditEventRepository;
import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository; import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository;
import org.springframework.boot.actuate.audit.listener.AbstractAuditListener;
import org.springframework.boot.actuate.audit.listener.AuditListener; import org.springframework.boot.actuate.audit.listener.AuditListener;
import org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener; import org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener;
import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener; import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener;
...@@ -48,6 +49,7 @@ public class AuditAutoConfiguration { ...@@ -48,6 +49,7 @@ public class AuditAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean(AbstractAuditListener.class)
public AuditListener auditListener() throws Exception { public AuditListener auditListener() throws Exception {
return new AuditListener(this.auditEventRepository); return new AuditListener(this.auditEventRepository);
} }
......
...@@ -18,8 +18,10 @@ package org.springframework.boot.actuate.autoconfigure; ...@@ -18,8 +18,10 @@ package org.springframework.boot.actuate.autoconfigure;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.audit.AuditEventRepository; import org.springframework.boot.actuate.audit.AuditEventRepository;
import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository; import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository;
import org.springframework.boot.actuate.audit.listener.AbstractAuditListener;
import org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener; import org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener;
import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener; import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener;
import org.springframework.boot.actuate.security.AuthenticationAuditListener; import org.springframework.boot.actuate.security.AuthenticationAuditListener;
...@@ -44,7 +46,7 @@ public class AuditAutoConfigurationTests { ...@@ -44,7 +46,7 @@ public class AuditAutoConfigurationTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Test @Test
public void testTraceConfiguration() throws Exception { public void defaultConfiguration() throws Exception {
registerAndRefresh(AuditAutoConfiguration.class); registerAndRefresh(AuditAutoConfiguration.class);
assertThat(this.context.getBean(AuditEventRepository.class)).isNotNull(); assertThat(this.context.getBean(AuditEventRepository.class)).isNotNull();
assertThat(this.context.getBean(AuthenticationAuditListener.class)).isNotNull(); assertThat(this.context.getBean(AuthenticationAuditListener.class)).isNotNull();
...@@ -52,7 +54,7 @@ public class AuditAutoConfigurationTests { ...@@ -52,7 +54,7 @@ public class AuditAutoConfigurationTests {
} }
@Test @Test
public void ownAutoRepository() throws Exception { public void ownAuditEventRepository() throws Exception {
registerAndRefresh(CustomAuditEventRepositoryConfiguration.class, registerAndRefresh(CustomAuditEventRepositoryConfiguration.class,
AuditAutoConfiguration.class); AuditAutoConfiguration.class);
assertThat(this.context.getBean(AuditEventRepository.class)) assertThat(this.context.getBean(AuditEventRepository.class))
...@@ -75,6 +77,14 @@ public class AuditAutoConfigurationTests { ...@@ -75,6 +77,14 @@ public class AuditAutoConfigurationTests {
.isInstanceOf(TestAuthorizationAuditListener.class); .isInstanceOf(TestAuthorizationAuditListener.class);
} }
@Test
public void ownAuditListener() throws Exception {
registerAndRefresh(CustomAuditListenerConfiguration.class,
AuditAutoConfiguration.class);
assertThat(this.context.getBean(AbstractAuditListener.class))
.isInstanceOf(TestAuditListener.class);
}
private void registerAndRefresh(Class<?>... annotatedClasses) { private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses); this.context.register(annotatedClasses);
this.context.refresh(); this.context.refresh();
...@@ -139,4 +149,23 @@ public class AuditAutoConfigurationTests { ...@@ -139,4 +149,23 @@ public class AuditAutoConfigurationTests {
} }
@Configuration
protected static class CustomAuditListenerConfiguration {
@Bean
public TestAuditListener testAuditListener() {
return new TestAuditListener();
}
}
protected static class TestAuditListener extends AbstractAuditListener {
@Override
protected void onAuditEvent(AuditEvent event) {
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment