001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.actuate.autoconfigure.audit;
018
019import org.springframework.beans.factory.ObjectProvider;
020import org.springframework.boot.actuate.audit.AuditEvent;
021import org.springframework.boot.actuate.audit.AuditEventRepository;
022import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository;
023import org.springframework.boot.actuate.audit.listener.AbstractAuditListener;
024import org.springframework.boot.actuate.audit.listener.AuditListener;
025import org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener;
026import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener;
027import org.springframework.boot.actuate.security.AuthenticationAuditListener;
028import org.springframework.boot.actuate.security.AuthorizationAuditListener;
029import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
030import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
031import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
032import org.springframework.context.annotation.Bean;
033import org.springframework.context.annotation.Configuration;
034
035/**
036 * {@link EnableAutoConfiguration Auto-configuration} for {@link AuditEvent}s.
037 *
038 * @author Dave Syer
039 * @author Vedran Pavic
040 * @since 2.0.0
041 */
042@Configuration
043public class AuditAutoConfiguration {
044
045        private final AuditEventRepository auditEventRepository;
046
047        public AuditAutoConfiguration(
048                        ObjectProvider<AuditEventRepository> auditEventRepository) {
049                this.auditEventRepository = auditEventRepository.getIfAvailable();
050        }
051
052        @Bean
053        @ConditionalOnMissingBean(AbstractAuditListener.class)
054        public AuditListener auditListener() throws Exception {
055                return new AuditListener(this.auditEventRepository);
056        }
057
058        @Bean
059        @ConditionalOnClass(name = "org.springframework.security.authentication.event.AbstractAuthenticationEvent")
060        @ConditionalOnMissingBean(AbstractAuthenticationAuditListener.class)
061        public AuthenticationAuditListener authenticationAuditListener() throws Exception {
062                return new AuthenticationAuditListener();
063        }
064
065        @Bean
066        @ConditionalOnClass(name = "org.springframework.security.access.event.AbstractAuthorizationEvent")
067        @ConditionalOnMissingBean(AbstractAuthorizationAuditListener.class)
068        public AuthorizationAuditListener authorizationAuditListener() throws Exception {
069                return new AuthorizationAuditListener();
070        }
071
072        @Configuration
073        @ConditionalOnMissingBean(AuditEventRepository.class)
074        protected static class AuditEventRepositoryConfiguration {
075
076                @Bean
077                public InMemoryAuditEventRepository auditEventRepository() throws Exception {
078                        return new InMemoryAuditEventRepository();
079                }
080
081        }
082
083}