“This is the 22nd day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Access System and RBAC Model overview [Absolute Classic] Solution implementation steps are as follows:

Basic privileges database table

  1. The users table
CREATE TABLE `sys_user` (
  `user_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL COMMENT 'Username',
  `password` varchar(100) DEFAULT NULL COMMENT 'password',
  `salt` varchar(20) DEFAULT NULL COMMENT 'salt',
  `email` varchar(100) DEFAULT NULL COMMENT 'email',
  `mobile` varchar(100) DEFAULT NULL COMMENT 'Mobile phone Number',
  `status` tinyint(4) DEFAULT NULL COMMENT 'Status 0: Disabled 1: Normal',
  `create_user_id` bigint(20) DEFAULT NULL COMMENT 'creator ID',
  `create_time` datetime DEFAULT NULL COMMENT 'Creation time'.PRIMARY KEY (`user_id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='System User';
Copy the code
  1. Menu table (Permission table)
CREATE TABLE `sys_menu` (
  `menu_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `parent_id` bigint(20) DEFAULT NULL COMMENT 'Parent menu ID, primary menu 0',
  `name` varchar(50) DEFAULT NULL COMMENT 'Menu name',
  `url` varchar(200) DEFAULT NULL COMMENT 'menu URL',
  `perms` varchar(500) DEFAULT NULL COMMENT 'Authorization (multiple comma separated, e.g. User :list,user:create)',
  `type` int(11) DEFAULT NULL COMMENT 'Type 0: Directory 1: Menu 2: button',
  `icon` varchar(50) DEFAULT NULL COMMENT 'Menu icon',
  `order_num` int(11) DEFAULT NULL COMMENT 'order'.PRIMARY KEY (`menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COMMENT='Menu Management';
Copy the code
  1. Character sheet
CREATE TABLE `sys_role` (
  `role_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `role_name` varchar(100) DEFAULT NULL COMMENT 'Role Name',
  `remark` varchar(100) DEFAULT NULL COMMENT 'note',
  `create_user_id` bigint(20) DEFAULT NULL COMMENT 'creator ID',
  `create_time` datetime DEFAULT NULL COMMENT 'Creation time'.PRIMARY KEY (`role_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT=The 'role';
Copy the code
  1. Role and menu association table
CREATE TABLE `sys_role_menu` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `role_id` bigint(20) DEFAULT NULL COMMENT 'character ID',
  `menu_id` bigint(20) DEFAULT NULL COMMENT 'menu ids'.PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COMMENT='Role and menu mapping';
Copy the code
  1. User and role association table
CREATE TABLE `sys_user_role` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) DEFAULT NULL COMMENT 'user ID',
  `role_id` bigint(20) DEFAULT NULL COMMENT 'character ID'.PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='User and Role mapping';
Copy the code

Two. Some main code

Two relationships need to be maintained: users and roles, and roles and permissions. After a user is successfully authenticated, the user can query all menus and permissions of the current user in the table and return to the front end. The current end accessing an interface annotated with @RequiresperMissions also needs to query the table to see if the current user has access to the current method. When changing a role owned by a user, you need to delete the previously saved data about the user from the sys_USER_ROLE table and save it again. The same is true when changing the permissions of a role. For details, please refer to the link:

Github.com/Dr-Water/ra…

The main code is the class shown in the red box:

For springBoot and Shiro integration, and captcha analysis, please refer to the link:

  1. The way shiro customizes filters and tokens in the SpringBoot project

  2. Generating captcha using Google’s Kaptcha is super easy for the SpringBoot project