Notice that spring data is enough for tracing when your data is created or modified, but it is not enough to trace which user is creating or updating them. In that case, we also need spring security integration
To achieve this we will:
1. Create an abstract base entity model and put those fields in it so that all the entity model classes have those properties
2. Create a concrete entity model class and extend the abstract base entity model.
3. Implement the AuditorAware interface and set the auditorAware bean.
In this article, I will not show how to integrate with Spring security. I just assume you already have done that and you have your authentication object in Spring security context.
1. The Base EntityModel class
@MappedSuperclass@EntityListeners(AuditingEntityListener.class)
public class EntityModelBase implements Serializable {
@CreatedDate @Column(name = "CREATED_DATE", nullable = false, updatable = false)
protected long createdDate;
@LastModifiedDate @Column(name = "LAST_MODIFIED_DATE", nullable = false)
protected long lastModifiedDate;
@CreatedBy
@Column(name = "CREATED_BY")
protected String createdBy;
@LastModifiedBy
@Column(name = "LAST_MODIFIED_BY")
protected String lastModifiedBy;
...
2. A Concrete EntityModel class
@Entity@Table(name="ROLES")
public class RoleEntityModel extends EntityModelBase {
@Column(name = "NAME", nullable = false)
private String name;
...
3. The AuditorAware Bean
First, we need to create our own AuditorAware bean. Note that in this bean I used my own authentication accessor object that brings me the authentication object from Spring security context. Also, I used a custom authentication class (an extension of the spring security's UsernamePasswordAuthenticationToken) which holds extra information about the authenticated user that I need.
import org.springframework.data.domain.AuditorAware;
public class SpringSecurityAuditorAware implements AuditorAware {
@Autowired
private MyAuthenticationAccessor authAccessor;
@Override
public String getCurrentAuditor() {
MyUser user = this.authAccessor.getUser();
if (user != null) {
return user.getUsername();
}
return null;
}
}
@Component
public class MyAuthenticationAccessor {
@Override
public MyUser getUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth instanceof MyCustomAuthentication) {
return ((MyCustomAuthentication)authentication).getUser();
}
return null;
}
...
}
Finally, we need to set the AuditorAware bean. The best place to do that is in a @Configurationannotated class:@Configuration@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableTransactionManagement@EnableJpaRepositories(basePackages = "...")
public class VeriErisimKatmaniKonfigurasyonu {
@Bean
public AuditorAware auditorAware() {
return new SpringSecurityAuditorAware();
}
...
}
Now, when we save an entity model to the database, we will have its created date, created by, last modified date, and last modified by properties.
Hiç yorum yok:
Yorum Gönder