Rub*_*osa 0 java spring hibernate jpa spring-mvc
我正在尝试从 Spring Security 提供的 User 和 Authorities 获取属性。
我知道您可以覆盖 UserDetailsService 以创建您自己的自定义类,但在这种情况下,我想获得完整的默认类,另外还有一些属性。
让我用代码解释一下:
public class User implements UserDetails, CredentialsContainer {
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
private String password;
private final String username;
private final Set<GrantedAuthority> authorities;
private final boolean accountNonExpired;
private final boolean accountNonLocked;
private final boolean credentialsNonExpired;
private final boolean enabled;
}
Run Code Online (Sandbox Code Playgroud)
这是 Spring Security 提供的 User 类,我想要的是使用这些属性扩展我自己的类。我试过了,但数据库没有从用户那里获取属性:
@Entity
@Table(name="MyUser")
public class ExtendedUser extends User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String email;
private static final long serialVersionUID = -2147191899215904479L;
public ExtendedUser(String username, String password, boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked,
Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
// TODO Auto-generated constructor stub
}
.
.
.
Run Code Online (Sandbox Code Playgroud)
我的问题是,我是否需要创建自己的 User 类才能继承这些属性并使用 JPA 批注让 Hibernate “理解”它?
我想我在这里误导了一些东西,但我想不通。
在 JPA 中,将其映射到orm.xml或 Hibernate 中,您可以使用已弃用的HBM xml。
我实际上建议不要从 Spring SecurityUser
类扩展实体映射,因为它使您的映射与其实现非常耦合,您无法控制并且可能使升级变得更加困难。
相反,我建议您创建自己的User
实体并将信息存储在适合您的应用程序需求的模型中。然后作为实现的一部分UserDetailsService
,您获取User
实体和相关实体并构造一个扩展UserDetails
对象,并将其提供给 Spring Security。
归档时间: |
|
查看次数: |
2049 次 |
最近记录: |