当前位置:网站首页>Query permission information from database
Query permission information from database
2022-06-09 10:20:00 【Leon_ Jinhai_ Sun】
preparation
CREATE DATABASE /*!32312 IF NOT EXISTS*/`sg_security` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE `sg_security`;
/*Table structure for table `sys_menu` */
DROP TABLE IF EXISTS `sys_menu`;
CREATE TABLE `sys_menu` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`menu_name` varchar(64) NOT NULL DEFAULT 'NULL' COMMENT ' Menu name ',
`path` varchar(200) DEFAULT NULL COMMENT ' Routing address ',
`component` varchar(255) DEFAULT NULL COMMENT ' Component path ',
`visible` char(1) DEFAULT '0' COMMENT ' Menu status (0 Show 1 hide )',
`status` char(1) DEFAULT '0' COMMENT ' Menu status (0 normal 1 Discontinue use )',
`perms` varchar(100) DEFAULT NULL COMMENT ' Authority sign ',
`icon` varchar(100) DEFAULT '#' COMMENT ' Menu icons ',
`create_by` bigint(20) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_by` bigint(20) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`del_flag` int(11) DEFAULT '0' COMMENT ' Whether or not to delete (0 Not delete 1 deleted )',
`remark` varchar(500) DEFAULT NULL COMMENT ' remarks ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT=' Menu table ';
/*Table structure for table `sys_role` */
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL,
`role_key` varchar(100) DEFAULT NULL COMMENT ' Role permission string ',
`status` char(1) DEFAULT '0' COMMENT ' A status (0 normal 1 Discontinue use )',
`del_flag` int(1) DEFAULT '0' COMMENT 'del_flag',
`create_by` bigint(200) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_by` bigint(200) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`remark` varchar(500) DEFAULT NULL COMMENT ' remarks ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT=' Role table ';
/*Table structure for table `sys_role_menu` */
DROP TABLE IF EXISTS `sys_role_menu`;
CREATE TABLE `sys_role_menu` (
`role_id` bigint(200) NOT NULL AUTO_INCREMENT COMMENT ' role ID',
`menu_id` bigint(200) NOT NULL DEFAULT '0' COMMENT ' menu id',
PRIMARY KEY (`role_id`,`menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
/*Table structure for table `sys_user` */
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Primary key ',
`user_name` varchar(64) NOT NULL DEFAULT 'NULL' COMMENT ' user name ',
`nick_name` varchar(64) NOT NULL DEFAULT 'NULL' COMMENT ' nickname ',
`password` varchar(64) NOT NULL DEFAULT 'NULL' COMMENT ' password ',
`status` char(1) DEFAULT '0' COMMENT ' Account status (0 normal 1 Discontinue use )',
`email` varchar(64) DEFAULT NULL COMMENT ' mailbox ',
`phonenumber` varchar(32) DEFAULT NULL COMMENT ' cell-phone number ',
`sex` char(1) DEFAULT NULL COMMENT ' User's gender (0 male ,1 Woman ,2 Unknown )',
`avatar` varchar(128) DEFAULT NULL COMMENT ' Head portrait ',
`user_type` char(1) NOT NULL DEFAULT '1' COMMENT ' The user types (0 Administrators ,1 Ordinary users )',
`create_by` bigint(20) DEFAULT NULL COMMENT ' Creator's user id',
`create_time` datetime DEFAULT NULL COMMENT ' Creation time ',
`update_by` bigint(20) DEFAULT NULL COMMENT ' Updated by ',
`update_time` datetime DEFAULT NULL COMMENT ' Update time ',
`del_flag` int(11) DEFAULT '0' COMMENT ' Delete logo (0 Represents not deleted ,1 Represents the deletion of )',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT=' User table ';
/*Table structure for table `sys_user_role` */
DROP TABLE IF EXISTS `sys_user_role`;
CREATE TABLE `sys_user_role` (
`user_id` bigint(200) NOT NULL AUTO_INCREMENT COMMENT ' user id',
`role_id` bigint(200) NOT NULL DEFAULT '0' COMMENT ' role id',
PRIMARY KEY (`user_id`,`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;SELECT
DISTINCT m.`perms`
FROM
sys_user_role ur
LEFT JOIN `sys_role` r ON ur.`role_id` = r.`id`
LEFT JOIN `sys_role_menu` rm ON ur.`role_id` = rm.`role_id`
LEFT JOIN `sys_menu` m ON m.`id` = rm.`menu_id`
WHERE
user_id = 2
AND r.`status` = 0
AND m.`status` = 0import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
@TableName(value="sys_menu")
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Menu implements Serializable {
private static final long serialVersionUID = -54979041104113736L;
@TableId
private Long id;
/**
* Menu name
*/
private String menuName;
/**
* Routing address
*/
private String path;
/**
* Component path
*/
private String component;
/**
* Menu status (0 Show 1 hide )
*/
private String visible;
/**
* Menu status (0 normal 1 Discontinue use )
*/
private String status;
/**
* Authority sign
*/
private String perms;
/**
* Menu icons
*/
private String icon;
private Long createBy;
private Date createTime;
private Long updateBy;
private Date updateTime;
/**
* Whether or not to delete (0 Not delete 1 deleted )
*/
private Integer delFlag;
/**
* remarks
*/
private String remark;
}Code implementation
We just need to according to the user id You can query the corresponding permission information .
So we can first define mapper, Which provides a method according to userid Query permission information .
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sangeng.domain.Menu;
import java.util.List;
public interface MenuMapper extends BaseMapper<Menu> {
List<String> selectPermsByUserId(Long id);
}In particular, custom methods , So you need to create a corresponding mapper file , Define corresponding sql sentence
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sangeng.mapper.MenuMapper">
<select id="selectPermsByUserId" resultType="java.lang.String">
SELECT
DISTINCT m.`perms`
FROM
sys_user_role ur
LEFT JOIN `sys_role` r ON ur.`role_id` = r.`id`
LEFT JOIN `sys_role_menu` rm ON ur.`role_id` = rm.`role_id`
LEFT JOIN `sys_menu` m ON m.`id` = rm.`menu_id`
WHERE
user_id = #{userid}
AND r.`status` = 0
AND m.`status` = 0
</select>
</mapper>stay application.yml Middle configuration mapperXML The location of the file
spring:
datasource:
url: jdbc:mysql://localhost:3306/sg_security?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
redis:
host: localhost
port: 6379
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
Then we can go to UserDetailsServiceImpl To call the mapper The method of querying permission information is encapsulated into LoginUser In the object .
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Autowired
private MenuMapper menuMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getUserName,username);
User user = userMapper.selectOne(wrapper);
if(Objects.isNull(user)){
throw new RuntimeException(" Wrong user name or password ");
}
List<String> permissionKeyList = menuMapper.selectPermsByUserId(user.getId());
// // Test writing method
// List<String> list = new ArrayList<>(Arrays.asList("test"));
return new LoginUser(user,permissionKeyList);
}
}边栏推荐
- [genius_platform software platform development] lesson 101st: summary of errors encountered in compiling Windows environment vs2017 for DZ products of power projects
- ISCC2022 擂台misc
- [leetcode] [Niuke] brush questions on binary tree
- 1019. 链表中的下一个更大节点
- fabric-ca介绍,安装,使用
- 損失 3 億美元後,IBM 宣布退出俄羅斯!
- Machine learning notes - Interpretation of u-net papers
- fabric-ca介紹,安裝,使用
- Machine learning notes - explore the keras dataset
- 面试题 04.02. 最小高度树-深度优先遍历,加树的分治法
猜你喜欢

数字图像处理 图解图像恢复任务

Notes on the development of raspberry pie (15): Raspberry pie 4b+ compile and install MySQL database from the source code

Terrain learning summary (6) -- terrain practice based on Alibaba cloud platform

Digital image processing graphic image restoration task
![N-grams language model -- [torch learning notes]](/img/94/c5c4ac70590158b3c21d18c3b86a6f.png)
N-grams language model -- [torch learning notes]

Machine learning housing rental price forecasting: exploratory data analysis + Feature Engineering + modeling + reporting

Unsupportedoperationexception exception resolution

MSF practice - harm of ms17-010 vulnerability

MicroNet:以极低的 FLOP 实现图像识别

Machine learning notes - explore the keras dataset
随机推荐
如何管理云计算平台用户?
【genius_platform软件平台开发】第一万零一讲:电力项目dz产品windows环境vs2017编译遇到的报错汇总
Go strconv package
1019. the next larger node in the linked list
其它权限校验方法
Kubernetes Chapter 7: Advanced pod, advanced controller, resource and dashboard
106. construct binary tree from middle order and post order traversal sequence
UnsupportedOperationException异常解决
什么样的数字藏品平台才是好平台?
同花顺app交易安全吗
1331. array sequence number conversion - quick sort plus binary search
Custom permission verification method
Test development engineers who don't work overtime are not good programmers? It may not be a stupid bird, but it always flies first
山东济南的名人颜廷利东方著名哲学家及其思想,中国需要这样的思想家
面试题 01.06. 字符串压缩
Openstack explanation (18) -- Nova service startup and service creation
106. 从中序与后序遍历序列构造二叉树
序列模型——【torch学习笔记】
面试题 04.02. 最小高度树-深度优先遍历,加树的分治法
How many points can you get if the programmer's college entrance examination paper is exposed?