title: How to configure Auditing via Java Config in Spring Data (and Spring Data Rest)?
body: <p>I am trying to use Spring Data's auditing capabilities (in combination with Spring Boot and Spring Data Rest), but the audit fields are not being set on save. All saves result in a constraint exception from trying to save a null "Created By."</p>
  
  <p>According to the <a href="http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#auditing" rel="nofollow">spring data docs</a>, I should just be able to place the appropriate auditing annotations (@CreatedDate/etc) on my Entity, and make an AuditorAware&lt;> available to the application context. I know my auditor aware bean is being created from setting a breakpoint in the debugger.</p>
  
  <p>My questions are:</p>
  
  <p>1) Is it necessary for me to create an AuditingEntityListener, or should I expect one to be provided from having @EnableJpaAuditing? (it's not clear in the docs about java config)</p>
  
  <p>2) Is there other configuration in the below code that I'm missing to set up automatic auditing?</p>
  
  <p>3) I'm calling the creation code from a POST to Spring Data Rest, are there any special caveats with using this auditing functionality in combination with Spring Data Rest?</p>
  
  <pre><code>@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 ...
  </code></pre>
  
  <p>And the configuration:</p>
  
  <pre><code>@EnableJpaAuditing
  @Configuration
  public class AuditingConfig {
  
      @Bean
      public AuditorAware&lt;String&gt; createAuditorProvider() {
          return new SecurityAuditor();
      }
  
      @Bean
      public AuditingEntityListener createAuditingListener() {
          return new AuditingEntityListener();
      }
  
      public static class SecurityAuditor implements AuditorAware&lt;String&gt; {
          @Override
          public String getCurrentAuditor() {
              Authentication auth = SecurityContextHolder.getContext().getAuthentication();
              String username = auth.getName();
              return username;
          }
      }
  
  }
  </code></pre>
  
  <p>Any help is much appreciated, thanks!</p>