赞
踩
Role-Based Access Control(RBAC),基于角色的访问控制。
引用网图:
一个用户可具备多角色,一个角色可具备多权限。表述,一个用户有什么身份,能对资源做什么操作。
设计RBAC的实现的表结构图:
省略Getter/Setter、省略equals和hashCode的重写。
@Entity
public class SysUser implements UserDetails {
@Id
@GeneratedValue
private long id;
@Column(nullable = false, unique = true)
private String name;
private String password;
private String cnname;
private Boolean enabled = Boolean.TRUE;
//多对多映射,用户角色
@ManyToMany(cascade = {CascadeType.REFRESH}, fetch = FetchType.EAGER)
private List<SysRole> roles;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> authorities = new ArrayList<>();
List<SysRole> roles = this.getRoles();
for (SysRole role : roles) {
authorities.add(new SimpleGrantedAuthority(role.getRole()));
}
return authorities;
}
}
省略Getter/Setter、省略equals和hashCode的重写。
@Entity
public class SysRole {
@Id
@GeneratedValue
private Integer id;
private String cnname;
private String role; // 角色标识程序中判断使用,如"sys",这个是唯一的:
private String description; // 角色描述,UI界面显示使用
private Boolean available = Boolean.FALSE; // 是否可用,如果不可用将不会添加给用户
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "SysRolePermission", joinColumns = {@JoinColumn(name = "roleId")}, inverseJoinColumns = {@JoinColumn(name = "permissionId")})
private List<SysPermission> permissions; // 角色 -- 权限关系,多对多关系
@ManyToMany
@JoinTable(name = "SysUserRole", joinColumns = {@JoinColumn(name = "roleId")}, inverseJoinColumns = {@JoinColumn(name = "uid")})
private List<SysUser> userInfos; // 用户 - 角色关系定义,一个角色对应多个用户
}
省略Getter/Setter、省略equals和hashCode的重写。
@Entity
public class SysPermission implements Serializable {
@Id
@GeneratedValue
private Integer id;
private String name;
@Column(columnDefinition = "enum('menu','button')") // 资源类型,[menu|button]
private String resourceType;
private String url; // 资源路径
private String permission; // 权限字符串,menu例子:role:*,button例子:role:create,role:update,role:delete,role:view
private Long parentId; // 父编号
private String parentIds; // 父编号列表
private Boolean available = Boolean.FALSE;
@Transient
private List<?> permissions;
@ManyToMany
@JoinTable(name = "SysRolePermission", joinColumns = {@JoinColumn(name = "permissionId")}, inverseJoinColumns = {@JoinColumn(name = "roleId")})
private List<SysRole> roles;
public List<?> getPermissions() {
return Arrays.asList(this.permission.trim().split("\\|"));
}
public void setPermissions(List<?> permissions) {
this.permissions = permissions;
}
}
https://github.com/lmmarise/springboot-security-jpa-rbac.git
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。