SpringBoot+SpringSecurity+MybatisPlus+Vue3小项目摘录(五)
2023-12-31 08:28:47
获取用户角色权限信息实现
提示:这里需要先把用户角色权限信息表的处理,基本的用户角色权限通过五张基础的数据表的实现,分别是 用户信息表、角色信息表、权限信息表、用户角色信息表、角色权限信息表
用户角色权限的sql初始化语句
/*
Navicat Premium Data Transfer
Source Server : 2003
Source Server Type : MySQL
Source Server Version : 50527 (5.5.27)
Source Host : localhost:3306
Source Schema : security_vue3
Target Server Type : MySQL
Target Server Version : 50527 (5.5.27)
File Encoding : 65001
Date: 27/12/2023 22:35:04
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for sys_menu
-- ----------------------------
DROP TABLE IF EXISTS `sys_menu`;
CREATE TABLE `sys_menu` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '菜单主键ID',
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '菜单名称',
`icon` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '#' COMMENT '菜单图标',
`parent_id` bigint(20) NULL DEFAULT NULL COMMENT '父菜单ID',
`order_num` int(11) NULL DEFAULT 0 COMMENT '显示顺序',
`path` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '路由地址',
`component` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '组件路径',
`menu_type` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '菜单类型(M目录 C菜单 F按钮)',
`perms` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '权限标识',
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 34 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_menu
-- ----------------------------
INSERT INTO `sys_menu` VALUES (1, '系统管理', 'system', 0, 1, '/sys', '', 'M', '', '2023-11-04 14:56:29', '2023-11-04 14:56:29', '系统管理目录');
INSERT INTO `sys_menu` VALUES (2, '业务管理', 'monitor', 0, 2, '/bsns', '', 'M', '', '2023-11-04 14:56:29', '2023-11-04 14:56:29', '业务管理目录');
INSERT INTO `sys_menu` VALUES (3, '用户管理', 'user', 1, 1, '/sys/user', 'sys/user/index', 'C', 'system:user:list', '2023-11-04 14:56:29', '2023-11-04 14:56:29', '用户管理菜单');
INSERT INTO `sys_menu` VALUES (4, '角色管理', 'peoples', 1, 2, '/sys/role', 'sys/role/index', 'C', 'system:role:list', '2023-11-04 14:56:29', '2023-11-04 14:56:29', '角色管理菜单');
INSERT INTO `sys_menu` VALUES (5, '菜单管理', 'tree-table', 1, 3, '/sys/menu', 'sys/menu/index', 'C', 'system:menu:list', '2023-11-04 14:56:30', '2023-11-04 14:56:30', '菜单管理菜单');
INSERT INTO `sys_menu` VALUES (6, '部门管理', 'tree', 2, 1, '/bsns/department', 'bsns/Department', 'C', '', '2023-11-04 14:56:31', '2023-11-04 14:56:31', '部门管理菜单');
INSERT INTO `sys_menu` VALUES (7, '岗位管理', 'post', 2, 2, '/bsns/post', 'bsns/Post', 'C', '', '2023-11-04 14:56:32', '2023-11-04 14:56:32', '岗位管理菜单');
INSERT INTO `sys_menu` VALUES (8, '用户新增', '#', 3, 2, '', '', 'F', 'system:user:add', '2023-11-04 14:56:33', '2023-11-04 14:56:33', '添加用户按钮');
INSERT INTO `sys_menu` VALUES (9, '用户修改', '#', 3, 3, '', '', 'F', 'system:user:edit', '2023-11-04 14:56:34', '2023-11-04 14:56:34', '修改用户按钮');
INSERT INTO `sys_menu` VALUES (10, '用户删除', '#', 3, 4, '', '', 'F', 'system:user:delete', '2023-11-04 14:56:35', '2023-11-04 14:56:35', '删除用户按钮');
INSERT INTO `sys_menu` VALUES (11, '分配角色', '#', 3, 5, '', '', 'F', 'system:user:role', '2023-11-04 14:56:36', '2023-11-04 14:56:36', '分配角色按钮');
INSERT INTO `sys_menu` VALUES (12, '重置密码', '#', 3, 6, '', '', 'F', 'system:user:resetPwd', '2023-11-04 14:56:37', '2023-11-04 14:56:37', '重置密码按钮');
INSERT INTO `sys_menu` VALUES (13, '角色新增', '#', 4, 2, '', '', 'F', 'system:role:add', '2023-11-04 14:56:37', '2023-11-04 14:56:37', '添加用户按钮');
INSERT INTO `sys_menu` VALUES (14, '角色修改', '#', 4, 3, '', '', 'F', 'system:role:edit', '2023-11-04 14:56:38', '2023-11-04 14:56:37', '修改用户按钮');
INSERT INTO `sys_menu` VALUES (15, '角色删除', '#', 4, 4, '', NULL, 'F', 'system:role:delete', '2023-11-04 14:56:39', '2023-11-04 14:56:39', '删除用户按钮');
INSERT INTO `sys_menu` VALUES (16, '分配权限', '#', 4, 5, '', '', 'F', 'system:role:menu', '2023-11-04 14:56:40', '2023-11-04 14:56:40', '分配权限按钮');
INSERT INTO `sys_menu` VALUES (17, '菜单新增', '#', 5, 2, '', NULL, 'F', 'system:menu:add', '2023-11-04 14:56:40', '2023-11-04 14:56:40', '添加菜单按钮');
INSERT INTO `sys_menu` VALUES (18, '菜单修改', '#', 5, 3, '', NULL, 'F', 'system:menu:edit', '2023-11-04 14:56:41', '2023-11-04 14:56:41', '修改菜单按钮');
INSERT INTO `sys_menu` VALUES (19, '菜单删除', '#', 5, 4, '', NULL, 'F', 'system:menu:delete', '2023-11-04 14:56:41', '2023-11-04 14:56:41', '删除菜单按钮');
INSERT INTO `sys_menu` VALUES (20, '用户查询', '#', 3, 1, '', NULL, 'F', 'system:user:query', '2023-11-04 14:56:42', '2023-11-04 14:56:42', '用户查询按钮');
INSERT INTO `sys_menu` VALUES (21, '角色查询', '#', 4, 1, '', NULL, 'F', 'system:role:query', '2023-11-04 14:56:43', '2023-11-04 14:56:43', '角色查询按钮');
INSERT INTO `sys_menu` VALUES (22, '菜单查询', '#', 5, 1, '', NULL, 'F', 'system:menu:query', '2023-11-04 14:56:45', '2023-11-04 14:56:45', '菜单查询按钮');
INSERT INTO `sys_menu` VALUES (33, '测速22', '122', 3, 3, '', '34', 'M', '33', '2023-11-04 14:56:46', '2023-11-04 14:56:46', '测试管理目录');
-- ----------------------------
-- Table structure for sys_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '角色主键ID',
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '角色名称',
`code` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '角色权限字符串',
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 22 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_role
-- ----------------------------
INSERT INTO `sys_role` VALUES (1, '超级管理员', 'admin', '2023-11-04 14:40:44', '2023-11-04 14:40:47', '拥有系统最高权限');
INSERT INTO `sys_role` VALUES (2, '普通角色', 'common', '2023-11-04 14:41:56', '2023-11-04 14:41:58', '普通角色');
INSERT INTO `sys_role` VALUES (3, '测试角色', 'test', '2023-11-04 14:42:24', '2023-11-04 14:42:27', '测试角色');
INSERT INTO `sys_role` VALUES (4, '2', NULL, NULL, NULL, NULL);
INSERT INTO `sys_role` VALUES (5, '3', NULL, NULL, NULL, NULL);
INSERT INTO `sys_role` VALUES (6, '4', NULL, NULL, NULL, NULL);
INSERT INTO `sys_role` VALUES (7, '5', NULL, NULL, NULL, NULL);
INSERT INTO `sys_role` VALUES (14, '6', NULL, NULL, NULL, NULL);
INSERT INTO `sys_role` VALUES (16, '8', NULL, NULL, NULL, NULL);
INSERT INTO `sys_role` VALUES (17, '0', NULL, NULL, NULL, NULL);
INSERT INTO `sys_role` VALUES (19, '测2', 'cc2', '2023-11-13 21:06:21', '2023-11-13 13:06:27', 'eewew2');
INSERT INTO `sys_role` VALUES (20, 'ccc测试', 'test2', '2023-11-29 17:10:33', NULL, 'xxx');
INSERT INTO `sys_role` VALUES (21, '今天测试角色', 'todytest', '2023-11-29 22:01:11', NULL, 'ccc');
-- ----------------------------
-- Table structure for sys_role_menu
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_menu`;
CREATE TABLE `sys_role_menu` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '角色菜单主键ID',
`role_id` bigint(20) NULL DEFAULT NULL COMMENT '角色ID',
`menu_id` bigint(20) NULL DEFAULT NULL COMMENT '菜单ID',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 239 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_role_menu
-- ----------------------------
INSERT INTO `sys_role_menu` VALUES (8, 2, 1);
INSERT INTO `sys_role_menu` VALUES (9, 2, 2);
INSERT INTO `sys_role_menu` VALUES (10, 2, 3);
INSERT INTO `sys_role_menu` VALUES (11, 2, 4);
INSERT INTO `sys_role_menu` VALUES (12, 2, 5);
INSERT INTO `sys_role_menu` VALUES (13, 2, 6);
INSERT INTO `sys_role_menu` VALUES (14, 2, 7);
INSERT INTO `sys_role_menu` VALUES (15, 3, 2);
INSERT INTO `sys_role_menu` VALUES (16, 3, 6);
INSERT INTO `sys_role_menu` VALUES (17, 3, 7);
INSERT INTO `sys_role_menu` VALUES (21, 7, 1);
INSERT INTO `sys_role_menu` VALUES (22, 7, 2);
INSERT INTO `sys_role_menu` VALUES (23, 7, 6);
INSERT INTO `sys_role_menu` VALUES (24, 7, 7);
INSERT INTO `sys_role_menu` VALUES (25, 6, 1);
INSERT INTO `sys_role_menu` VALUES (26, 6, 3);
INSERT INTO `sys_role_menu` VALUES (27, 6, 9);
INSERT INTO `sys_role_menu` VALUES (28, 6, 10);
INSERT INTO `sys_role_menu` VALUES (29, 19, 1);
INSERT INTO `sys_role_menu` VALUES (30, 19, 3);
INSERT INTO `sys_role_menu` VALUES (31, 19, 2);
INSERT INTO `sys_role_menu` VALUES (32, 19, 6);
INSERT INTO `sys_role_menu` VALUES (33, 1, 1);
INSERT INTO `sys_role_menu` VALUES (34, 1, 3);
INSERT INTO `sys_role_menu` VALUES (35, 1, 20);
INSERT INTO `sys_role_menu` VALUES (36, 1, 8);
INSERT INTO `sys_role_menu` VALUES (37, 1, 9);
INSERT INTO `sys_role_menu` VALUES (38, 1, 10);
INSERT INTO `sys_role_menu` VALUES (39, 1, 11);
INSERT INTO `sys_role_menu` VALUES (40, 1, 12);
INSERT INTO `sys_role_menu` VALUES (41, 1, 4);
INSERT INTO `sys_role_menu` VALUES (42, 1, 21);
INSERT INTO `sys_role_menu` VALUES (43, 1, 13);
INSERT INTO `sys_role_menu` VALUES (44, 1, 14);
INSERT INTO `sys_role_menu` VALUES (45, 1, 15);
INSERT INTO `sys_role_menu` VALUES (46, 1, 16);
INSERT INTO `sys_role_menu` VALUES (47, 1, 23);
INSERT INTO `sys_role_menu` VALUES (48, 1, 5);
INSERT INTO `sys_role_menu` VALUES (49, 1, 22);
INSERT INTO `sys_role_menu` VALUES (50, 1, 17);
INSERT INTO `sys_role_menu` VALUES (51, 1, 18);
INSERT INTO `sys_role_menu` VALUES (52, 1, 19);
INSERT INTO `sys_role_menu` VALUES (53, 1, 2);
INSERT INTO `sys_role_menu` VALUES (54, 1, 6);
INSERT INTO `sys_role_menu` VALUES (55, 1, 7);
INSERT INTO `sys_role_menu` VALUES (208, 20, 1);
INSERT INTO `sys_role_menu` VALUES (209, 20, 3);
INSERT INTO `sys_role_menu` VALUES (210, 20, 20);
INSERT INTO `sys_role_menu` VALUES (211, 20, 8);
INSERT INTO `sys_role_menu` VALUES (212, 20, 9);
INSERT INTO `sys_role_menu` VALUES (213, 20, 33);
INSERT INTO `sys_role_menu` VALUES (214, 20, 10);
INSERT INTO `sys_role_menu` VALUES (215, 20, 11);
INSERT INTO `sys_role_menu` VALUES (216, 20, 4);
INSERT INTO `sys_role_menu` VALUES (217, 20, 21);
INSERT INTO `sys_role_menu` VALUES (218, 20, 13);
INSERT INTO `sys_role_menu` VALUES (219, 20, 5);
INSERT INTO `sys_role_menu` VALUES (220, 20, 22);
INSERT INTO `sys_role_menu` VALUES (221, 20, 17);
INSERT INTO `sys_role_menu` VALUES (222, 20, 18);
INSERT INTO `sys_role_menu` VALUES (223, 20, 2);
INSERT INTO `sys_role_menu` VALUES (224, 20, 6);
INSERT INTO `sys_role_menu` VALUES (225, 20, 7);
INSERT INTO `sys_role_menu` VALUES (232, 21, 1);
INSERT INTO `sys_role_menu` VALUES (233, 21, 9);
INSERT INTO `sys_role_menu` VALUES (234, 21, 4);
INSERT INTO `sys_role_menu` VALUES (235, 21, 21);
INSERT INTO `sys_role_menu` VALUES (236, 21, 2);
INSERT INTO `sys_role_menu` VALUES (237, 21, 6);
INSERT INTO `sys_role_menu` VALUES (238, 21, 7);
-- ----------------------------
-- Table structure for sys_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
`password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`avatar` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT 'default.jpg' COMMENT '用户头像',
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '用户邮箱',
`phonenumber` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '手机号码',
`login_date` datetime NULL DEFAULT NULL COMMENT '最后登录时间',
`status` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT '帐号状态(0正常 1停用)',
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 31 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES (1, 'xuguoguo', '$2a$10$Kib4zuVhTzg3I1CoqJfd0unuY9G9ysI7cfbhyT3fi7k7Z/4pr3bGW', 'xuguoguo.jpg', 'xuguoguo0073@163.com', '18163615796', '2023-09-29 22:10:52', '0', '2023-09-09 08:47:52', '2023-09-22 08:47:54', '备注');
INSERT INTO `sys_user` VALUES (2, 'test', '$2a$10$Kib4zuVhTzg3I1CoqJfd0unuY9G9ysI7cfbhyT3fi7k7Z/4pr3bGW', 'default.jpg', '', '18937459285', '2023-09-30 18:47:01', '1', '2023-09-29 18:47:14', NULL, NULL);
INSERT INTO `sys_user` VALUES (3, 'test2', '$2a$10$tiArwm0GxChyEP5k0JGzsOuzyY15IKA.ZTl8S2aj3haYlKAfpwfl.', '333.jpg', '', '', '2023-11-24 17:36:07', '0', NULL, NULL, NULL);
INSERT INTO `sys_user` VALUES (4, '1', '$2a$10$lD0Fx7oMsFFmX9hVkmYy7eJteH8pBaXXro1X9DEMP5sbM.Z6Co55m', 'default.jpg', '', '', NULL, '1', NULL, NULL, NULL);
INSERT INTO `sys_user` VALUES (5, '2', NULL, 'default.jpg', '', '', NULL, '1', NULL, NULL, NULL);
INSERT INTO `sys_user` VALUES (15, 'fdsfs', '$2a$10$AQVcp4hQ7REc5o7ztVnI7eX.sJdcYy3d1x2jm5CfrcCoMZMPacfpi', 'default.jpg', '123454342344@qq.com', '18862851414', '2023-11-02 02:22:45', '1', '2023-11-02 02:21:24', '2023-11-01 18:23:16', '');
INSERT INTO `sys_user` VALUES (28, 'sdfss2', '$2a$10$7aNJxwVmefI0XAk64vrzYuOqeeImYJUQnoBrtKP9pLTGTWO2CXQ/y', 'default.jpg', '12343424@qq.com', '18862857413', NULL, '1', '2023-11-07 00:42:46', '2023-11-06 16:43:04', '');
INSERT INTO `sys_user` VALUES (29, 'ccc', '$2a$10$7cbWeVwDWO9Hh3qbJrvTHOn0E/DLYXxnIZpxZei0jY4ChfQbJuhi.', 'default.jpg', '754332242@qq.com', '18862584120', '2023-11-29 19:52:27', '0', '2023-11-29 17:04:58', NULL, '');
INSERT INTO `sys_user` VALUES (30, 'ccc666', '$2a$10$Tmw5VCM/K2vb837AZDYHQOqE3gPiRZKevxLsh/ozndpTSjdwABqaK', 'default.jpg', '7546454@qq.com', '18865259845', '2023-11-29 22:05:18', '0', '2023-11-29 22:00:39', NULL, '');
-- ----------------------------
-- Table structure for sys_user_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_user_role`;
CREATE TABLE `sys_user_role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户角色主键ID',
`user_id` bigint(20) NULL DEFAULT NULL COMMENT '用户ID',
`role_id` bigint(20) NULL DEFAULT NULL COMMENT '角色ID',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 23 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_user_role
-- ----------------------------
INSERT INTO `sys_user_role` VALUES (1, 1, 1);
INSERT INTO `sys_user_role` VALUES (2, 2, 2);
INSERT INTO `sys_user_role` VALUES (4, 1, 2);
INSERT INTO `sys_user_role` VALUES (6, 3, 3);
INSERT INTO `sys_user_role` VALUES (7, 3, 2);
INSERT INTO `sys_user_role` VALUES (9, 4, 3);
INSERT INTO `sys_user_role` VALUES (10, 5, 3);
INSERT INTO `sys_user_role` VALUES (11, 15, 3);
INSERT INTO `sys_user_role` VALUES (16, 28, 2);
INSERT INTO `sys_user_role` VALUES (17, 28, 3);
INSERT INTO `sys_user_role` VALUES (20, 29, 20);
INSERT INTO `sys_user_role` VALUES (21, 30, 17);
INSERT INTO `sys_user_role` VALUES (22, 30, 21);
SET FOREIGN_KEY_CHECKS = 1;
5张数据表的如下:
- 用户信息表sys_user
- 角色信息表sys_role
- 权限信息表sys_menu
- 用户角色信息表sys_user_role
- 角色权限信息表sys_role_menu
通过MybatisX插件将对应的模块的三层接口生成
面向对象思想编程
仔细观察每个实体对象,发现多个对象中的属性【数据表的列】都是相同的
如:id、create_time、update_time、create_user、update_user、remark,所以我们可以创建一个通用基础实体类对象,
然后让其他类去继承这个基类对象即可,这样的话方便程序的维护。
基类对象BaseEntity的创建
package com.xuguoguo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 公共基础实体类
* @author xuguoguo
*/
@Data
public class BaseEntity implements Serializable {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 创建日期
*/
@JsonSerialize(using=CustomDateTimeSerializer.class)
@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")
private Date createTime;
/**
* 更新日期
*/
@JsonSerialize(using=CustomDateTimeSerializer.class)
@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/**
* 备注
*/
private String remark;
}
编写时间对象CustomDateTimeSerializer进行解析处理(时间需要进行处理)
package com.xuguoguo.entity;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* 自定义返回JSON 数据格式中日期格式化处理
* @author xuguoguo
*
*/
public class CustomDateTimeSerializer extends JsonSerializer<Date>{
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
gen.writeString(sdf.format(value));
}
}
最后在几个实体对象中先继承自BaseEntity,并将对象中的几个重复属性进行注释或者删除即可
修改自定义登录逻辑中的获取权限信息
修改common包下的MyUserDetailsServiceImpl类对象的权限列表代码
注意需要修改MyJwtAuthenticationFilter过滤器中的用户编号查询权限列表的方法
service业务层的编写
service实现类代码:
package com.xuguoguo.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuguoguo.entity.SysMenu;
import com.xuguoguo.entity.SysRole;
import com.xuguoguo.entity.SysUser;
import com.xuguoguo.mapper.SysMenuMapper;
import com.xuguoguo.mapper.SysRoleMapper;
import com.xuguoguo.service.SysUserService;
import com.xuguoguo.mapper.SysUserMapper;
import com.xuguoguo.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author xuguoguo
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2023-12-05 20:20:08
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
implements SysUserService{
@Autowired
private SysRoleMapper sysRoleMapper;
@Autowired
private SysMenuMapper sysMenuMapper;
@Override
public String getUserAuthority(Long userId) {
StringBuffer authority = new StringBuffer();
//根据用户id获取所有的角色信息
// 角色信息 ROLE_admin、ROLE_common
// 权限信息 system:user:query、system:role:query、system:user:list
List<SysRole> roleList = sysRoleMapper.selectList(new QueryWrapper<SysRole>().inSql("id",
"select role_id from sys_user_role where user_id=" + userId));
if (roleList.size()>0) {
String roleCode = roleList.stream().map(r -> "ROLE_" + r.getCode()).collect(Collectors.joining(","));
authority.append(roleCode);
}
//遍历所有的角色,获取所有的权限信息并且不重复
Set<String> menuCodeSet = new HashSet<>();
for (SysRole role : roleList) {
List<SysMenu> menuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().inSql("id",
"select menu_id from sys_role_menu where role_id=" + role.getId()));
for (SysMenu menu : menuList) {
String perms = menu.getPerms();
if (StringUtil.isNotEmpty(perms)) {
menuCodeSet.add(perms);
}
}
}
if (menuCodeSet.size()>0) {
authority.append(",");
String menuCode = menuCodeSet.stream().collect(Collectors.joining(","));
authority.append(menuCode);
}
System.out.println("authority:"+authority.toString());
return authority.toString();
}
@Override
public SysUser getByUsername(String username) {
return getOne(new QueryWrapper<SysUser>().eq("username",username));
}
}
查询的sql演变:
分别启动服务器进行登录测试,查看控制台的用户权限是否正确
此时控制器中的请求,只要登录了就可以进行访问
请求方法上注解的使用
1、在请求方法上添加必须拥有某个角色才能请求进来的注解
@PreAuthorize("hasRole('ROLE_admin22')")
这样的话会经过全局的异常处理器进行返回【返回的数据可以自行修改哦】
2、在请求方法上添加必须拥有某个权限才能请求进来的注解
@PreAuthorize("hasAuthority('system:test:aa')")
登录页面的记住密码功能实现
实现思路
用户登录时若勾选“记住我”功能选项,则将登录名和密码(加密后)保存至本地缓存中,下次登录页面加载时自动获取保存好的账号和密码(需解密),回显到登录输入框中。
这里有三种方法来存储账号密码:
- 1.sessionStorage(不推荐)
- 1). 仅在当前会话下有效,关闭浏览器窗口后就被清除了
- 2). 存放数据大小一般为5MB
- 3). 不与服务器进行交互通信
- 2.localStorage
- 1). 除非主动清除localStorage里的信息,否则将永远存在,关闭浏览器窗口后下次启动任然存在
- 2). 存放数据大小一般为5MB
- 3). 不与服务器进行交互通信
- 3.cookies
- 1). 可以手动设置过期时间,超过有效期则失效。未设置过期时间,关闭浏览器窗口后就被清除了
- 2). 存放数据大小一般为4K
- 3). 每次请求都会被传送到服务器
早期这种功能性操作我们都是使用的cookie来保存在客户端浏览器进行存储,然后进行回显即可
这里我们需要安装依赖【js-cookie】来处理实现
存储用户密码,为了安全需要进行加密处理,获取密码解密操作,需要安装依赖【jsencrypt】
在util目录下新建jsencrypt.js
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='
const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
'7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
'UP8iWi1Qw0Y='
// 加密
export function encrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey) // 设置公钥
return encryptor.encrypt(txt) // 对数据进行加密
}
// 解密
export function decrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPrivateKey(privateKey) // 设置私钥
return encryptor.decrypt(txt) // 对数据进行解密
}
在需要使用记住密码的vue页面【Login.vue】中引入
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/util/jsencrypt";
修改声明以及数据绑定
在登录处理函数中,添加逻辑代码
if (loginForm.value.rememberMe) {
//勾选了需要记住密码设置在cookkie中设置记住用户名和密码【单位是天】
Cookies.set("username", loginForm.value.username, { expires: 30 });
Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
} else {
// 否则移除
Cookies.remove("username");
Cookies.remove("password");
Cookies.remove("rememberMe");
}
添加从Cookies中获得账户和解密后密码的函数
//获取cookie的函数
function getCookie() {
const username = Cookies.get("username");
const password = Cookies.get("password");
const rememberMe = Cookies.get("rememberMe");
loginForm.value = {
username: username === undefined ? loginForm.value.username : username,
password: password === undefined ? loginForm.value.password :decrypt(password),
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
};
}
//调用获取cookie
getCookie();
访问登录页面,在登录的时间勾选记住密码复选框,下次请求登录时可以看到已经记住了账号信息
小结
提示:本小结中主体实现的是使用数据库的用户信息实现登录,通过面向对象的编程思想实现代码的抽取base类对象以及登录页面的记住密码的实现引导处理!!!
本章的第五小节完毕,敬请期待后续更新(可留言需要学习哪方面的内容哈)!如果需要源码或者工具的朋友们可关注微信公众号"锅锅编程生活"或者扫描二维码关注回复关键字/后台留言获取即可!
文章来源:https://blog.csdn.net/xu772710255/article/details/135257415
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!