@CreatedBy 和 @LastModifiedBy 设置实际实体而不是 id [英] @CreatedBy and @LastModifiedBy set actual entity instead of id

查看:60
本文介绍了@CreatedBy 和 @LastModifiedBy 设置实际实体而不是 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体看起来像:

I have an entity which looks like:

@Audited
@Data
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

  public static final long UNSAVED = 0;

  @Id
  @GeneratedValue
  private long id;

  @CreatedDate
  @Column(name = "created_at", updatable = false)
  private ZonedDateTime createdAt;

  @CreatedBy
  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "created_by")
  private User createdBy;

  @LastModifiedDate
  private ZonedDateTime updatedAt;

  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "updated_by")
  @LastModifiedBy
  private User updatedBy;

}

我想要@LastModifiedBy 和@CreatedBy,以便他们设置相应的用户.但是,当我尝试保存实体时,出现异常:

I'm want @LastModifiedBy and @CreatedBy so that they set corresponding users. However, when I try to save the entity, I get an exception:

java.lang.ClassCastException: Cannot cast java.lang.Long to com.intranet.users.Users

所以在我看来,它试图设置的不是实际用户,而是 id.有没有办法让 spring 在实体上设置实际的用户,而不仅仅是它的 id?

So it seems to me that it tries to set not actual User, but it's id. Is there any way to make spring set actual User on the entity and not just it's id?

谢谢

推荐答案

文档:

如果您使用@CreatedBy 或@LastModifiedBy,审计基础设施需要以某种方式了解当前的委托人.为此,我们提供了一个 AuditorAware SPI 接口,您必须实现告诉基础设施谁是当前用户或系统与应用程序交互是.泛型类型 T 定义了什么键入用 @CreatedBy 或 @LastModifiedBy 注释的属性成为.

In case you use either @CreatedBy or @LastModifiedBy, the auditing infrastructure somehow needs to become aware of the current principal. To do so, we provide an AuditorAware SPI interface that you have to implement to tell the infrastructure who the current user or system interacting with the application is. The generic type T defines what type the properties annotated with @CreatedBy or @LastModifiedBy have to be.

以下示例显示了该接口的实现使用 Spring Security 的 Authentication 对象:

The following example shows an implementation of the interface that uses Spring Security’s Authentication object:

示例 104. 基于 Spring Security 的 AuditorAware 实现

Example 104. Implementation of AuditorAware based on Spring Security

class SpringSecurityAuditorAware implements AuditorAware<User> {

  public Optional<User> getCurrentAuditor() {

    return Optional.ofNullable(SecurityContextHolder.getContext())
        .map(SecurityContext::getAuthentication)
        .filter(Authentication::isAuthenticated)
        .map(Authentication::getPrincipal)
        .map(User.class::cast);   
  } 
} 

实现访问Spring Security提供的Authentication对象,查找您在您的应用程序中创建的自定义 UserDetails 实例UserDetailsS​​ervice 实现.我们在这里假设你是通过 UserDetails 实现暴露域用户,但是那,根据发现的身份验证,你也可以查一下来自任何地方.

The implementation accesses the Authentication object provided by Spring Security and looks up the custom UserDetails instance that you have created in your UserDetailsService implementation. We assume here that you are exposing the domain user through the UserDetails implementation but that, based on the Authentication found, you could also look it up from anywhere.

这篇关于@CreatedBy 和 @LastModifiedBy 设置实际实体而不是 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆