栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

RBAC权限模型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RBAC权限模型

目录

1.基本概念

 2.实现


1.基本概念

全写(Role-Based Access Control)基于角色的访问控制

操作系统的各种权限不直接赋予具体用户,而是在用户集合和权限集合中间创立一个角色集合,每一种角色对应一组响应的权限,一旦用户被分配适当的角色后,该用户就拥有此角色的所有权限功能,减少系统开销

 

 2.实现

创建三个表

用户表

-- auto-generated definition
create table tb_user
(
    id          int unsigned auto_increment comment '主键'
        primary key,
    open_id     varchar(200)    null comment '长期授权字符串',
    nickname    varchar(200)    null comment '昵称',
    photo       varchar(200)    null comment '头像网址',
    name        varchar(20)     null comment '姓名',
    sex         enum ('男', '女') null comment '性别',
    tel         char(11)        null comment '手机号码',
    email       varchar(200)    null comment '邮箱',
    hiredate    date            null comment '入职日期',
    role        json            not null comment '角色',
    root        tinyint(1)      not null comment '是否是超级管理员',
    dept_id     int unsigned    null comment '部门编号',
    status      tinyint         not null comment '状态',
    create_time datetime        not null on update CURRENT_TIMESTAMP comment '创建时间',
    constraint unq_open_id
        unique (open_id)
)
    comment '用户表' charset = utf8;

create index idx_dept_id
    on tb_user (dept_id);

create index idx_status
    on tb_user (status);

create index unq_email
    on tb_user (email);

 

行为表

create table tb_permission
(
    id              int unsigned auto_increment comment '主键'
        primary key,
    permission_name varchar(200) not null comment '权限',
    module_id       int unsigned not null comment '模块ID',
    action_id       int unsigned not null comment '行为ID',
    constraint unq_complex
        unique (module_id, action_id),
    constraint unq_permission
        unique (permission_name)
)
    charset = utf8;

 

角色表

create table tb_role
(
    id          int unsigned auto_increment comment '主键'
        primary key,
    role_name   varchar(200) not null comment '角色名称',
    permissions json         not null comment '权限集合',
    constraint unq_role_name
        unique (role_name)
)
    comment '角色表' charset = utf8;

 Mysql5.7之后引入JSON数据格式,字段permission。支持索引

可以保存两种数据1.json数据,2.数组

进行查询

#  举例 创建json数组
SELECT JSON_ARRAY (10,20,30);
# 查询包含100
SELECT JSON_CONTAINS(JSON_ARRAY(10,20,30), "100");

select distinct p.permission_name
from tb_user u
join tb_role r on JSON_CONTAINS(u.role, CAST(r.id As CHAR ))
join tb_permission p on JSON_CONTAINS(r.permissions,CAST(p.id as char ))
where u.id=1 and u.status=1

暑期编程PK赛 得CSDN机械键盘等精美礼品!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号