How to configure audit through Java configuration in Spring Data (and Spring Data Rest)?

I am trying to use Spring's data audit capabilities (in combination with Spring Boot and Spring Data Rest), but audit fields are not set when saved. All saved results result in exclusion of the restriction from the attempt to keep the zero β€œCreated”.

According to spring data docs , I just have to put the appropriate audit annotations (@ CreatedDate / etc) on my Entity and make AuditorAware <> available to the application context. I know that my auditor knows that a bean is created from setting a breakpoint in the debugger.

My questions:

1) Do I need to create an AuditingEntityListener, or should I expect it to be provided from @EnableJpaAuditing? (this is not clear in the java config docs)

2) Is there any other configuration in the code below that I don’t have enough to set up automatic auditing?

3) I call the creation code from POST to Spring Data Rest, are there any special caveats using this audit function in conjunction with Spring Data Rest?

@Entity public class Tag implements Serializable { // ... other fields omitted... @CreatedDate @Temporal(TemporalType.TIMESTAMP) private Date created = new Date(); @CreatedBy @Basic(optional = false) @Column(name = "CREATED_BY", nullable = false, length = 24) private String createdBy = ""; @LastModifiedDate @Basic(optional = false) @Column(nullable = false) @Temporal(TemporalType.TIMESTAMP) private Date updated = new Date(); @LastModifiedBy @Basic(optional = false) @Column(name = "UPDATED_BY", nullable = false, length = 24) private String updatedBy = ""; // ... getters and setters were generated ... 

And the configuration:

 @EnableJpaAuditing @Configuration public class AuditingConfig { @Bean public AuditorAware<String> createAuditorProvider() { return new SecurityAuditor(); } @Bean public AuditingEntityListener createAuditingListener() { return new AuditingEntityListener(); } public static class SecurityAuditor implements AuditorAware<String> { @Override public String getCurrentAuditor() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String username = auth.getName(); return username; } } } 

Any help is much appreciated, thanks!

+6
source share
1 answer

1) Do I need to create an AuditingEntityListener, or should I expect it to be provided from @EnableJpaAuditing? (this is not clear in the java config docs)

Answer: No, you do not need to define an AuditingEntityListener bean. Instead, you need to specify @EntityListeners(AuditingEntityListener.class) in your domain class.

eg.

 @Entity @EntityListeners(AuditingEntityListener.class) public class Tag implements Serializable { } 

2) Is there any other configuration in the code below that I don’t have enough to set up automatic auditing?

Answer: Other configuration settings look fine.

3) I call the creation code from POST to Spring Data Rest, are there any special caveats using this audit function in conjunction with Spring Data Rest?

Answer: I think not. Try the above change. It should just work.

+8
source

Source: https://habr.com/ru/post/986004/


All Articles