六,RBAC
RBAC(基于角色的权限控制 role base access control)是一种设计模式,是用来设计和管理权限相关数据的一种模型
RBAC权限数据的管理,都是重复的CRUD的操作,这里我们就不再重复的从0到1开发,我们只是把模块中需要注意的逻辑和代码讲一下,重复代码不再复述
RBAC模式:基于角色的访问控制,表结构如下:
6.2 需求分析
6.2.1 用户管理
用户列表
用户添加修改
6.2.2 角色管理
角色列表
角色修改
6.2.3 菜单管理
菜单列表
菜单修改
菜单不同于单表管理,是一个树状表结构管理,这里我们重点关注菜单(树状表结构的数据)的增删改查
6.3 导入权限数据管理工程
这里我们直接导入素材中的权限管理工程,因为业务比较复杂,不再从0到1开发,大家重点关注。重点模块的业务逻辑实现即可
6.4 实体类
package com.lxs.legou.security.po;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.lxs.legou.core.po.BaseEntity;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
@TableName("user_")
public class User extends BaseEntity {
@TableField("user_name_")
private String userName; //登录名
@TableField("real_name_")
private String realName; //真实姓名
@TableField("password_")
private String password;
@TableField("salt")
private String salt; //加密密码的盐
@TableField("sex_")
private String sex;
@TableField("tel_")
private String tel;
@TableField("email_")
private String email;
@TableField("desc_")
private String desc;
@TableField("lock_")
private Boolean lock; //是否锁定
@TableField("birthday_")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
@TableField("principal_")
private Boolean principal; //是否为部门负责人,用于"常用语:直接上级"
@TableField("dept_id_")
private Long deptId; //部门
@TableField("post_id_")
private Long postId; //岗位
@TableField(exist = false)
private String deptName; //部门名称,用于列表显示
@TableField(exist = false)
private Long[] roleIds; //瞬时属性,用户的角色列表,如:[1,3,4,5]
@TableField(exist = false)
private String postName; //部门名称,用于列表显示
}
package com.lxs.legou.security.po;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.lxs.legou.core.po.BaseEntity;
import lombok.Data;
/**
* @Title:
* @Description:
*
* @Copyright 2019 lxs - Powered By 雪松
* @Author: lxs
* @Date: 2019/10/9
* @Version V1.0
*/
@Data
@TableName("role_")
public class Role extends BaseEntity {
@TableField("name_")
private String name;
@TableField("title_")
private String title;
@TableField("desc_")
private String desc;
@TableField(exist = false)
private Long[] userIds; //瞬时属性,角色的用户列表,如:[1,3,4,5]
}
package com.lxs.legou.security.po;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.lxs.legou.core.po.BaseTreeEntity;
import lombok.Data;
/**
* @file Menu.java
* @Copyright (C) http://www.lxs.com
* @author lxs
* @email lxosng77@163.com
* @date 2018/7/13
*/
@Data
@TableName("menu_")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Menu extends BaseTreeEntity {
@TableField("path_")
private String path;
@TableField("name_")
private String name;
@TableField("component_")
private String component;
@TableField("hide_in_menu_")
private Boolean hideInMenu = false; //设为true后在左侧菜单不会显示该页面选项
@TableField("not_cache_")
private Boolean notCache = false; //设为true后页面不会缓存
@TableField("icon_")
private String icon; //该页面在左侧菜单、面包屑和标签导航处显示的图标,如果是自定义图标,需要在图标名称前加下划线'_'
@TableField(exist = false)
private Long roleId; //查询条件,拥有查询角色的菜单
@TableField(exist = false)
private Long userId; //查询条件,拥有查询用户的菜单
@TableField(exist = false)
private boolean selected; //角色选择菜单,选中角色已有的菜单
}
这里需要重点注意Menu实体类,因为是树状结构的数据需要继承BaseTreeEntity,通过
parent_id进行自身的关联
6.5 DAO和映射文件
参考具体导入的代码,实现具体用户角色,菜单的CRUD操作,因为代码较多这里不再转帖
6.6 Service
package com.lxs.legou.security.service;
import com.lxs.legou.core.service.ICrudService;
import com.lxs.legou.security.po.Role;
import com.lxs.legou.security.po.User;
import java.util.List;
/**
* @Title:
* @Description:
*
* @Copyright 2019 lxs - Powered By 雪松
* @Author: lxs
* @Date: 2019/10/9
* @Version V1.0
*/
public interface IUserService extends ICrudService<User> {
/**
* 根据用户id查询角色
* @param id
* @return
*/
public List<Role> selectRoleByUser(Long id);
/**
* 根据用户名,查询用户个数
* @param userName
* @return
*/
public Integer findCountByUserName(String userName);
/**
* 根据用户名查询用户
* @param userName
* @return
*/
public User getUserByUserName(String userName);
}
package com.lxs.legou.security.service;
import com.lxs.legou.core.service.ICrudService;
import com.lxs.legou.security.po.Role;
import com.lxs.legou.security.po.User;
import java.util.List;
/**
* @Title:
* @Description:
*
* @Copyright 2019 lxs - Powered By 雪松
* @Author: lxs
* @Date: 2019/10/9
* @Version V1.0
*/
public interface IRoleService extends ICrudService<Role> {
/**
* 查询角色的用户
* @param id
* @return
*/
public List<User> selectUserByRole(Long id);
}
package com.lxs.legou.security.service;
import com.lxs.legou.core.service.ICrudService;
import com.lxs.legou.security.po.Menu;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Title:
* @Description:
*
* @Copyright 2019 lxs - Powered By 雪松
* @Author: lxs
* @Date: 2019/10/9
* @Version V1.0
*/
@Service
public interface IMenuService extends ICrudService<Menu> {
/*