使用默认 spring-security 用户和休眠的对象是否需要自定义用户实现

do objects that use the default spring-security user and hibernate need a custom user implementation

我有基本的 spring 安全 login/logout 设置, 我有一个对象 MyObject,它的属性 created_by 与创建该对象的用户的 user_id。我还让 REST 调用正常工作,以便我获得当前正在调用列表函数的用户的对象作为属性。

我现在要做的是过滤 user_id 创建的对象。为了让休眠与此一起工作,我认为我必须在对象 created_by 属性中定义用户,因为休眠与实体一起工作。

例如:

public class MyObject implements Serializable {
    ...
    @Column(name = "created_by")
    org.springframework.security.core.userdetails.User created_by; 
    ... 
}

但是当我现在列出我得到的所有 MyObject 对象时:

 org.hibernate.type.SerializationException: could not deserialize

我尝试在 created_by 成员上使用 @OneToMany 但后来我得到:

 MyObject.created_by references an unknown entity: org.springframework.security.core.userdetails.User

由于它在没有用户输入的情况下工作,因此用户对象的序列化应该存在一些问题。我重新生成了其他对象的 serial_IDs 以确保问题仍然存在。有没有什么我错过了或者这不可能,我需要实现一个自定义用户来做我想做的事。

Spring 安全性不附带任何 JPA 支持,因此如果您想查询用户属性,您必须创建自己的用户实体。如果你只需要查询用户名,你可以这样做快捷方式:

@Column
private String created_by;