我正试图在Hibernate/JPA2中使用Spring Security数据库身份验证来实现DAO。 Spring使用下面的关系和关联,以表示用户&角色:如何使用Hibernate/JPA2实现Spring Security用户/权限?
repesented了PostgreSQL创建查询:
CREATE TABLE users
(
username character varying(50) NOT NULL,
"password" character varying(50) NOT NULL,
enabled boolean NOT NULL,
CONSTRAINT users_pkey PRIMARY KEY (username)
);
CREATE TABLE authorities
(
username character varying(50) NOT NULL,
authority character varying(50) NOT NULL,
CONSTRAINT fk_authorities_users FOREIGN KEY (username)
REFERENCES users (username) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
使用的GrantedAuthorities
,UserDetailsService
和UserDetailsmanager
板载实现,一切很好。但是,我对Spring的JDBC实现并不满意,并想写我自己的。为了做到这一点,我试图通过以下业务对象来创建关系的表示:
用户实体:
@Entity
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = {"username"})})
public class AppUser implements UserDetails, CredentialsContainer {
private static final long serialVersionUID = -8275492272371421013L;
@Id
@Column(name = "username", nullable = false, unique = true)
private String username;
@Column(name = "password", nullable = false)
@NotNull
private String password;
@OneToMany(
fetch = FetchType.EAGER, cascade = CascadeType.ALL,
mappedBy = "appUser"
)
private Set<AppAuthority> appAuthorities;
@Column(name = "accountNonExpired")
private Boolean accountNonExpired;
@Column(name = "accountNonLocked")
private Boolean accountNonLocked;
@Column(name = "credentialsNonExpired")
private Boolean credentialsNonExpired;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "personalinformation_fk", nullable = true)
@JsonIgnore
private PersonalInformation personalInformation;
@Column(name = "enabled", nullable = false)
@NotNull
private Boolean enabled;
public AppUser(
String username,
String password,
boolean enabled,
boolean accountNonExpired,
boolean credentialsNonExpired,
boolean accountNonLocked,
Collection<? extends AppAuthority> authorities,
PersonalInformation personalInformation
) {
if (((username == null) || "".equals(username)) || (password == null)) {
throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
}
this.username = username;
this.password = password;
this.enabled = enabled;
this.accountNonExpired = accountNonExpired;
this.credentialsNonExpired = credentialsNonExpired;
this.accountNonLocked = accountNonLocked;
this.appAuthorities = Collections.unmodifiableSet(sortAuthorities(authorities));
this.personalInformation = personalInformation;
}
public AppUser() {
}
@JsonIgnore
public PersonalInformation getPersonalInformation() {
return personalInformation;
}
@JsonIgnore
public void setPersonalInformation(PersonalInformation personalInformation) {
this.personalInformation = personalInformation;
}
// Getters, setters 'n other stuff
和管理局实体GrantedAuthorities的实现:
@Entity
@Table(name = "authorities", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class AppAuthority implements GrantedAuthority, Serializable {
//~ Instance fields ================================================================================================
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "authority", nullable = false)
private String authority;
// Here comes the buggy attribute. It is supposed to repesent the
// association username<->username, but I just don't know how to
// implement it
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "appuser_fk")
private AppUser appUser;
//~ Constructors ===================================================================================================
public AppAuthority(String username, String authority) {
Assert.hasText(authority,
"A granted authority textual representation is required");
this.username = username;
this.authority = authority;
}
public AppAuthority() {
}
// Getters 'n setters 'n other stuff
我的问题是@ManyToOne
assoc。 AppAuthorities
:它应该是“用户名”,但尝试和这样做会引发错误,因为我必须将该属性表示为String
......而Hibernate期望关联的实体。所以我尝试的实际上是提供了正确的实体,并通过@JoinColumn(name = "appuser_fk")
创建关联。这当然是垃圾,因为为了加载用户,我将在username
中有外键,而Hibernate在appuser_fk
中搜索它,它总是空的。
所以,这里是我的问题:关于如何修改上面提到的代码以获得数据模型的正确JPA2实现的任何建议?
感谢
啊!这真是个好消息!我会按照你的建议进行,谢谢。 – 2010-09-13 13:50:02
你可以分享项目的网址吗?我已经浪费了5天时间,使用spring引导安全rest api和oauth2来创建一个模块。问题是当我打电话给URL是给虚假的数据,如“权限”:null, “accountNonExpired”:false, “accountNonLocked”:false, “credentialsNonExpired”:false, “enabled”:false。 – Harsh 2017-10-17 06:39:25