@EventListener public void onAuditEvent(AuditApplicationEvent event) { AuditEvent auditEvent = event.getAuditEvent(); if (auditEvent.getType() == AuthenticationAuditListener.AUTHENTICATION_SUCCESS) { return; } log.info("Logging audit application event: timestamp: {}, principal: {}, type: {}, data: {}", auditEvent.getTimestamp(), auditEvent.getPrincipal(), auditEvent.getType(), auditEvent.getData() ); auditLogRepository.save( AuditLog.builder() .type(auditEvent.getType()) .principal(auditEvent.getPrincipal()) .timestamp(auditEvent.getTimestamp().toInstant()) .data(auditEvent.getData()) .build() ); }
@Test public void defaultConfiguration() throws Exception { registerAndRefresh(AuditAutoConfiguration.class); assertThat(this.context.getBean(AuditEventRepository.class)).isNotNull(); assertThat(this.context.getBean(AuthenticationAuditListener.class)).isNotNull(); assertThat(this.context.getBean(AuthorizationAuditListener.class)).isNotNull(); }
@Test public void testTraceConfiguration() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(AuditAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(AuditEventRepository.class)); assertNotNull(this.context.getBean(AuthenticationAuditListener.class)); assertNotNull(this.context.getBean(AuthorizationAuditListener.class)); }
@Bean @ConditionalOnClass(name = "org.springframework.security.authentication.event.AbstractAuthenticationEvent") @ConditionalOnMissingBean(AbstractAuthenticationAuditListener.class) public AuthenticationAuditListener authenticationAuditListener() throws Exception { return new AuthenticationAuditListener(); }
@Bean @ConditionalOnClass(name = "org.springframework.security.authentication.event.AbstractAuthenticationEvent") public AuthenticationAuditListener authenticationAuditListener() throws Exception { return new AuthenticationAuditListener(); }