我使用spring security并在启动身份验证过程时遇到一些问题
这是我的班级用户
@Entity
@Table(name="MEMBRE")
public class Membre implements UserDetails, Serializable {
...................
private ArrayList<Role> authorities;
@ManyToMany
@JoinTable(
name="MEMBRE_ROLE",
joinColumns={@JoinColumn(name="id_membre", referencedColumnName="id_membre")},
inverseJoinColumns={@JoinColumn(name="id_role", referencedColumnName="id_role")})
public Collection<Role> getAuthorities() {
return this.authorities;
}
public void setAuthorities(ArrayList<Role> authorities) {
this.authorities = authorities;
}
................
}
这是我的班级角色(GrantedAuthority)
@Entity
@Table(name="ROLE")
public class Role implements GrantedAuthority, Serializable {
/**
*
*/
private static final long serialVersionUID = 4160725609927520747L;
private Integer id;
private String role;
@Transient
public String getAuthority() {
return this.role;
}
@Id
@GeneratedValue
@Column(name = "id_role", unique = true, nullable = false, precision = 9, scale = 0)
public Integer getId() {
return this.id;
}
@Column(name = "role", nullable = false, length = 20)
public String getRole() {
return this.role;
}
public void setId(Integer id) {
this.id = id;
}
public void setRole(String role) {
this.role = role;
}
}
这是我的表MEMBRE_ROLE
CREATE TABLE IF NOT EXISTS `MEMBRE_ROLE` (
`id_role` INT NOT NULL AUTO_INCREMENT ,
`id_membre` INT NOT NULL),
PRIMARY KEY (`id_role`, `id_membre`) ,
CONSTRAINT `fk_membre_role_membre`
FOREIGN KEY (`id_membre` )
REFERENCES `membre` (`id_membre` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_membre_role_role`
FOREIGN KEY (`id_role` )
REFERENCES `role` (`id_role` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
当我尝试进行身份验证时,出现以下错误
org.springframework.security.authentication.InternalAuthenticationServiceException:调用属性的setter时发生IllegalArgumentException [domain.Membre.authorities(expected type = java.util.ArrayList)]; target = [domain.Membre@1807f3f2],property value = [[domain.Role@16cc0706,domain.Role@88b48]] domain.Membre.authorities的setter;嵌套异常是在调用setter for property [domain.Membre.authorities(expected type = java.util.ArrayList)]时发生的IllegalArgumentException; target = [domain.Membre@1807f3f2],财产价值= [[domain.Role@16cc0706,domain.Role@88b48]]
我知道setter没有正确定义,但是我可以'看看如何管理它,以及是否考虑到弹簧安全性来正确定义权限的映射
答案 0 :(得分:1)
为了不混淆JPA,对field,getter和setter使用相同的类型:
private Collection<Role> authorities;
public Collection<Role> getAuthorities() {
return this.authorities;
}
public void setAuthorities(Collection<Role> authorities) {
this.authorities = authorities;
}
(为简洁起见,省略了JPA注释。)
答案 1 :(得分:0)
我尝试通过
调试我的setter方法 public void setAuthorities(Object authorities) {
this.authorities = (ArrayList<Role>)authorities;
}
我得到一个PersistentBag作为当局的论据 这是权威表达的检查
如何将ArrayList作为参数?我想映射存在问题。
我在互联网上发现这篇文章,但它并没有给我一个有效的解决方案
http://www.coderanch.com/t/431759/ORM/databases/JPA-returning-collection-type-PersistentBag
答案 2 :(得分:0)
最后我找到了一个临时解决方案,不是很优雅,但它确实有效。如果你有更好的解决方案..........
public void setAuthorities(PersistentBag authorities) {
final List<Object> listObject = Arrays.asList(authorities.toArray());
final ArrayList<Role> roles = new ArrayList<Role>();
for (final Object object : listObject) {
if (object instanceof Role) {
roles.add((Role)object);
}
}
this.authorities = roles;
}