3.3.2 自定义UserDetails类:
当实体对象字段不满足时Spring Security认证时,需要自定义UserDetails。
3.3.3 注意点:
3.3.3.1. 将User类实现UserDetails接口
3.3.3.2. 将原有的isAccountNonExpired、isAccountNonLocked,isCredentialsNonExpired和isEnabled属性修 改成boolean类型,同时添加authorities属性。你可以点击userdetail去看看,里面都是boolean类型,不是Boolean类型的。
注意:这五个是集成spring security必须要用到的UserDetails类几个属性.
————————————————
@TableName("sys_user")
public class User implements Serializable, UserDetails {
//省略原有的属性......
/**
* 帐户是否过期(1 未过期,0已过期)
*/
private boolean isAccountNonExpired = true;
/**
* 帐户是否被锁定(1 未过期,0已过期)
*/
private boolean isAccountNonLocked = true;
/**
* 密码是否过期(1 未过期,0已过期)
*/
private boolean isCredentialsNonExpired = true;
/**
* 帐户是否可用(1 可用,0 删除用户)
*/
private boolean isEnabled = true;
/**
* 权限列表
*/
@TableField(exist = false)
Collection<? extends GrantedAuthority> authorities;
}
实际项目开发的完整user实体类:
```@TableName(value ="sys_user")
@Data
public class User implements Serializable , UserDetails {
/**用户编号
*/
@TableId(type = IdType.AUTO)
private Long id;/**
登录名称(用户名)
*/
private String username;/**
登录密码
*/
private String password;/**
帐户是否过期(1-未过期,0-已过期)
*/
private boolean isAccountNonExpired = true;/**
帐户是否被锁定(1-未过期,0-已过期)
*/
private boolean isAccountNonLocked = true;/**
密码是否过期(1-未过期,0-已过期)
*/
private boolean isCredentialsNonExpired =true;/**
帐户是否可用(1-可用,0-禁用)
*/
private boolean isEnabled =true;/**
真实姓名
*/
private String realName;/**
昵称
*/
private String nickName;/**
所属部门ID
*/
private Long departmentId;/**
所属部门名称
*/
private String departmentName;/**
性别(0-男,1-女)
*/
private Integer gender;/**
电话
*/
private String phone;/**
邮箱
*/
private String email;/**
用户头像
*/
private String avatar;/**
是否是管理员(1-管理员)
*/
private Integer isAdmin;/**
创建时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date createTime;/**
修改时间
*/
@TableField(fill = FieldFill.UPDATE)
private Date updateTime;/**
- 是否删除(0-未删除,1-已删除)
/
private Integer isDelete;
/* - 权限列表
/
@TableField(exist = false)
Collection<? extends GrantedAuthority> authorities;
/* - 查询用户权限列表
*/
@TableField(exist = false)
private List permissionList;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
User other = (User) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getUsername() == null ? other.getUsername() == null : this.getUsername().equals(other.getUsername()))
&& (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword()))
&& (this.getRealName() == null ? other.getRealName() == null : this.getRealName().equals(other.getRealName()))
&& (this.getNickName() == null ? other.getNickName() == null : this.getNickName().equals(other.getNickName()))
&& (this.getDepartmentId() == null ? other.getDepartmentId() == null : this.getDepartmentId().equals(other.getDepartmentId()))
&& (this.getDepartmentName() == null ? other.getDepartmentName() == null : this.getDepartmentName().equals(other.getDepartmentName()))
&& (this.getGender() == null ? other.getGender() == null : this.getGender().equals(other.getGender()))
&& (this.getPhone() == null ? other.getPhone() == null : this.getPhone().equals(other.getPhone()))
&& (this.getEmail() == null ? other.getEmail() == null : this.getEmail().equals(other.getEmail()))
&& (this.getAvatar() == null ? other.getAvatar() == null : this.getAvatar().equals(other.getAvatar()))
&& (this.getIsAdmin() == null ? other.getIsAdmin() == null : this.getIsAdmin().equals(other.getIsAdmin()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
&& (this.getIsDelete() == null ? other.getIsDelete() == null : this.getIsDelete().equals(other.getIsDelete()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode());
result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode());
result = prime * result + ((getRealName() == null) ? 0 : getRealName().hashCode());
result = prime * result + ((getNickName() == null) ? 0 : getNickName().hashCode());
result = prime * result + ((getDepartmentId() == null) ? 0 : getDepartmentId().hashCode());
result = prime * result + ((getDepartmentName() == null) ? 0 : getDepartmentName().hashCode());
result = prime * result + ((getGender() == null) ? 0 : getGender().hashCode());
result = prime * result + ((getPhone() == null) ? 0 : getPhone().hashCode());
result = prime * result + ((getEmail() == null) ? 0 : getEmail().hashCode());
result = prime * result + ((getAvatar() == null) ? 0 : getAvatar().hashCode());
result = prime * result + ((getIsAdmin() == null) ? 0 : getIsAdmin().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
result = prime * result + ((getIsDelete() == null) ? 0 : getIsDelete().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", username=").append(username);
sb.append(", password=").append(password);
sb.append(", isAccountNonExpired=").append(isAccountNonExpired);
sb.append(", isAccountNonLocked=").append(isAccountNonLocked);
sb.append(", isCredentialsNonExpired=").append(isCredentialsNonExpired);
sb.append(", isEnabled=").append(isEnabled);
sb.append(", realName=").append(realName);
sb.append(", nickName=").append(nickName);
sb.append(", departmentId=").append(departmentId);
sb.append(", departmentName=").append(departmentName);
sb.append(", gender=").append(gender);
sb.append(", phone=").append(phone);
sb.append(", email=").append(email);
sb.append(", avatar=").append(avatar);
sb.append(", isAdmin=").append(isAdmin);
sb.append(", createTime=").append(createTime);
sb.append(", updateTime=").append(updateTime);
sb.append(", isDelete=").append(isDelete);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
```