@Bean public AuditEventRepository auditEventRepository() { return new AuditEventRepository() { @Inject private AuditEventConverter auditEventConverter; @Override public List<AuditEvent> find(String principal, Date after) { Iterable<PersistentAuditEvent> persistentAuditEvents; if (principal == null && after == null) { persistentAuditEvents = persistenceAuditEventRepository.findAll(); } else if (after == null) { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipal(principal); } else { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipalAndAuditEventDateAfter(principal, new LocalDateTime(after)); } return auditEventConverter.convertToAuditEvent(persistentAuditEvents); } @Override @Transactional(propagation = Propagation.REQUIRES_NEW) public void add(AuditEvent event) { PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent(); persistentAuditEvent.setPrincipal(event.getPrincipal()); persistentAuditEvent.setAuditEventType(event.getType()); persistentAuditEvent.setAuditEventDate(new LocalDateTime(event.getTimestamp())); persistentAuditEvent.setData(auditEventConverter.convertDataToStrings(event.getData())); persistenceAuditEventRepository.save(persistentAuditEvent); } }; }
@Test public void testStoredEvents() { AuditEventRepository repository = mock(AuditEventRepository.class); AuditEvent event = new AuditEvent("principal", "type", Collections.<String, Object>emptyMap()); AuditListener listener = new AuditListener(repository); listener.onApplicationEvent(new AuditApplicationEvent(event)); verify(repository).add(event); }
@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 ownAuditEventRepository() throws Exception { registerAndRefresh(CustomAuditEventRepositoryConfiguration.class, AuditAutoConfiguration.class); assertThat(this.context.getBean(AuditEventRepository.class)) .isInstanceOf(TestAuditEventRepository.class); }
@Bean public AuditEventRepository auditEventRepository() { return new AuditEventRepository() { @Inject private AuditEventConverter auditEventConverter; @Override public List<AuditEvent> find(String principal, Date after) { Iterable<PersistentAuditEvent> persistentAuditEvents; if (principal == null && after == null) { persistentAuditEvents = persistenceAuditEventRepository.findAll(); } else if (after == null) { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipal(principal); } else { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipalAndAuditEventDateAfter(principal,DateToLocalDateTimeConverter.INSTANCE.convert(after)); } return auditEventConverter.convertToAuditEvent(persistentAuditEvents); } @Override @Transactional(propagation = Propagation.REQUIRES_NEW) public void add(AuditEvent event) { PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent(); persistentAuditEvent.setPrincipal(event.getPrincipal()); persistentAuditEvent.setAuditEventType(event.getType()); persistentAuditEvent.setAuditEventDate(JSR310DateConverters.DateToLocalDateTimeConverter.INSTANCE.convert(event.getTimestamp()) ); persistentAuditEvent.setData(auditEventConverter.convertDataToStrings(event.getData())); persistenceAuditEventRepository.save(persistentAuditEvent); } }; }
@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)); }
@Test public void ownAutoRepository() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(Config.class, AuditAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(AuditEventRepository.class), instanceOf(TestAuditEventRepository.class)); }
@Bean public AuditEventRepository auditEventRepository() { return new AuditEventRepository() { @Inject private AuditEventConverter auditEventConverter; @Override public List<AuditEvent> find(String principal, Date after) { final Iterable<PersistentAuditEvent> persistentAuditEvents; if (principal == null && after == null) { persistentAuditEvents = persistenceAuditEventRepository.findAll(); } else if (after == null) { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipal(principal); } else { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipalAndAuditEventDateGreaterThan(principal, new LocalDateTime(after)); } return auditEventConverter.convertToAuditEvent(persistentAuditEvents); } @Override public void add(AuditEvent event) { PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent(); persistentAuditEvent.setPrincipal(event.getPrincipal()); persistentAuditEvent.setAuditEventType(event.getType()); persistentAuditEvent.setAuditEventDate(new LocalDateTime(event.getTimestamp())); persistentAuditEvent.setData(auditEventConverter.convertDataToStrings(event.getData())); persistenceAuditEventRepository.save(persistentAuditEvent); } }; }
@Bean public AuditEventRepository auditEventRepository() { return new AuditEventRepository() { @Inject private AuditEventConverter auditEventConverter; @Override public List<AuditEvent> find(String principal, Date after) { Iterable<PersistentAuditEvent> persistentAuditEvents; if (principal == null && after == null) { persistentAuditEvents = persistenceAuditEventRepository.findAll(); } else if (after == null) { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipal(principal); } else { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipalAndAuditEventDateAfter(principal, new LocalDateTime(after)); } return auditEventConverter.convertToAuditEvent(persistentAuditEvents); } @Override public void add(AuditEvent event) { PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent(); persistentAuditEvent.setPrincipal(event.getPrincipal()); persistentAuditEvent.setAuditEventType(event.getType()); persistentAuditEvent.setAuditEventDate(new LocalDateTime(event.getTimestamp())); persistentAuditEvent.setData(auditEventConverter.convertDataToStrings(event.getData())); persistenceAuditEventRepository.save(persistentAuditEvent); } }; }
@Bean public AuditEventRepository auditEventRepository() { return new AuditEventRepository() { @Inject private AuditEventConverter auditEventConverter; @Override public List<AuditEvent> find(String principal, Date after) { final List<PersistentAuditEvent> persistentAuditEvents; if (principal == null && after == null) { persistentAuditEvents = persistenceAuditEventRepository.findAll(); } else if (after == null) { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipal(principal); } else { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipalAndAuditEventDateGreaterThan(principal, new LocalDateTime(after)); } return auditEventConverter.convertToAuditEvent(persistentAuditEvents); } @Override public void add(AuditEvent event) { PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent(); persistentAuditEvent.setPrincipal(event.getPrincipal()); persistentAuditEvent.setAuditEventType(event.getType()); persistentAuditEvent.setAuditEventDate(new LocalDateTime(event.getTimestamp())); persistentAuditEvent.setData(auditEventConverter.convertDataToStrings(event.getData())); persistenceAuditEventRepository.save(persistentAuditEvent); } }; }
@Bean public AuditEventRepository auditEventRepository() { return new AuditEventRepository() { private static final String AUTHORIZATION_FAILURE = "AUTHORIZATION_FAILURE"; private static final String ANONYMOUS_USER = "anonymousUser"; @Inject private AuditEventConverter auditEventConverter; @Override public List<AuditEvent> find(String principal, Date after) { Iterable<PersistentAuditEvent> persistentAuditEvents; if (principal == null && after == null) { persistentAuditEvents = persistenceAuditEventRepository.findAll(); } else if (after == null) { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipal(principal); } else { persistentAuditEvents = persistenceAuditEventRepository.findByPrincipalAndAuditEventDateAfter(principal, LocalDateTime.from(after.toInstant())); } return auditEventConverter.convertToAuditEvent(persistentAuditEvents); } @Override @Transactional(propagation = Propagation.REQUIRES_NEW) public void add(AuditEvent event) { if (!AUTHORIZATION_FAILURE.equals(event.getType()) && !ANONYMOUS_USER.equals(event.getPrincipal().toString())) { PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent(); persistentAuditEvent.setPrincipal(event.getPrincipal()); persistentAuditEvent.setAuditEventType(event.getType()); Instant instant = Instant.ofEpochMilli(event.getTimestamp().getTime()); persistentAuditEvent.setAuditEventDate(LocalDateTime.ofInstant(instant, ZoneId.systemDefault())); persistentAuditEvent.setData(auditEventConverter.convertDataToStrings(event.getData())); persistenceAuditEventRepository.save(persistentAuditEvent); } } }; }
public AuditListener(AuditEventRepository auditEventRepository) { this.auditEventRepository = auditEventRepository; }
public AuditAutoConfiguration( ObjectProvider<AuditEventRepository> auditEventRepositoryProvider) { this.auditEventRepository = auditEventRepositoryProvider.getIfAvailable(); }