目录
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机械键盘等精美礼品!



