前言【一套简单的登录、鉴权工具】无论是SpringSecruity、Shiro,对于一些小项目来说都太过复杂,有些情况下我们就想使用简单的登录、鉴权功能,本文记录手写一套简单的登录、鉴权工具
思路
1、封装工具类,集成查询系统用户、系统角色,根据登录用户权限进行当前URL请求鉴权
2、在拦截器中调用工具类进行鉴权,通过放行、不通过则抛出对应业务异常信息
首先需要三张基础表:系统用户表、系统角色表、用户角色关联表
-- ------------------------------ Table structure for sys_user-- ----------------------------DROP TABLE IF EXISTS `sys_user`;CREATE TABLE `sys_user`(`id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '表id',`nick_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '昵称',`user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '账号',`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '系统用户表' ROW_FORMAT = Compact;-- ------------------------------ Records of sys_user-- ----------------------------INSERT INTO `sys_user` VALUES ('1', '系统管理员', 'admin', '000000');INSERT INTO `sys_user` VALUES ('2', '张三-部门经理', 'zhangsan', '111111');INSERT INTO `sys_user` VALUES ('3', '小芳-前台接待', 'xiaofang', '222222');-- ------------------------------ Table structure for sys_role-- ----------------------------DROP TABLE IF EXISTS `sys_role`;CREATE TABLE `sys_role`(`id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '表id',`role_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '角色名称',`role_menu` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '角色菜单可视权限(可以不关联菜单,单独做成菜单管理直接与用户关联)',`role_url` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '角色URL访问权限',PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '系统角色表' ROW_FORMAT = Compact;-- ------------------------------ Records of sys_role-- ----------------------------INSERT INTO `sys_role` VALUES ('1', '管理员', '[{"menuName":"系统管理","menuPath":"/sys/xtgl"},{"menuName":"用户管理","menuPath":"/sys/yhgl"},{"menuName":"网站门户管理","menuPath":"/portal/mhgl"}]', '/sys/*,/portal/mhgl,/getLoginUser');INSERT INTO `sys_role` VALUES ('2', '部门领导', '[{"menuName":"用户管理","menuPath":"/sys/yhgl"},{"menuName":"网站门户管理","menuPath":"/portal/mhgl"}]', '/sys/yhgl,/portal/mhgl,/getLoginUser');INSERT INTO `sys_role` VALUES ('3', '普通员工', '[{"menuName":"网站门户管理","menuPath":"/portal/mhgl"}]', '/portal/mhgl,/getLoginUser');-- ------------------------------ Table structure for sys_user_role-- ----------------------------DROP TABLE IF EXISTS `sys_user_role`;CREATE TABLE `sys_user_role`(`id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '表id',`user_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户id',`role_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '角色id',PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '系统用户-角色关联表' ROW_FORMAT = Compact;-- ------------------------------ Records of sys_user_role-- ----------------------------INSERT INTO `sys_user_role` VALUES ('1', '1', '1');INSERT INTO `sys_user_role` VALUES ('2', '1', '2');INSERT INTO `sys_user_role` VALUES ('3', '1', '3');INSERT INTO `sys_user_role` VALUES ('4', '2', '2');INSERT INTO `sys_user_role` VALUES ('5', '3', '3');
在工具类中定义三个实体类方便传参接参(如果嫌麻烦也可以直接使用Map对象),使用自定义DbUtil查询数据库表数据(此操作,应交由项目ORM框架负责)
代码编写DbUtil工具类
package cn.huanzi.qch.util;import JAVA.sql.*;import java.util.ArrayList;import java.util.HashMap;/** * 原生jdbc操作数据库工具类 */public class DbUtil {//数据库连接:地址、用户名、密码private final String url;private final String username;private final String password;//Connection连接实例private Connection connection;public DbUtil(String url, String username, String password){this.url = url;this.username = username;this.password = password;}public DbUtil(String url, String username, String password, String driver){this(url,username,password);//加载驱动try {/*同时需要引入相关驱动依赖1、MySQL:com.mysql.cj.jdbc.Driver2、Oracle:oracle.jdbc.driver.OracleDriver3、pgsql:org.postgresql.Driver*/Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}/*** 获取 Connection 连接*/private Connection getConnection() {if(connection == null){try {connection= DriverManager.getConnection(url, username, password);connection.setAutoCommit(true);} catch (SQLException e) {System.err.println("获取Connection连接异常...");e.printStackTrace();}}return connection;}/*** 设置是否自动提交事务* 当需要进行批量带事务的操作时,关闭自动提交手动管理事务,将会大大提高效率!*/public void setAutoCommit(boolean autoCommit){try {this.getConnection().setAutoCommit(autoCommit);} catch (SQLException e) {e.printStackTrace();}}/*** 关闭自动提交事务时,需要手动管理事务提交、回滚*/public void commit(){try {this.getConnection().commit();} catch (SQLException e) {e.printStackTrace();}}public void rollback(){try {this.getConnection().rollback();} catch (SQLException e) {e.printStackTrace();}}/*** 关闭 Connection 连接*/public void close(){if(connection != null){try {connection.close();connection = null;} catch (SQLException e) {System.err.println("关闭Connection连接异常...");e.printStackTrace();}}}/*** 查询* 查询语句*/public ArrayList<HashMap<String,Object>> find(String sql, Object[] params) {ArrayList<HashMap<String, Object>> list = new ArrayList<>();//获取连接Connection conn = this.getConnection();PreparedStatement ps;ResultSet rs;try {//设置SQL、以及参数ps = conn.prepareStatement(sql);if (params != null) {for (int i = 0; i < params.length; i++) {ps.setObject(i + 1, params[i]);}}//执行查询rs = ps.executeQuery();//获取查询结果ResultSetMetaData rm = rs.getMetaData();int columnCount = rm.getColumnCount();//封装结果集while (rs.next()) {HashMap<String, Object> map = new HashMap<>(columnCount);for (int i = 1; i <= columnCount; i++) {String name = rm.getColumnName(i).toLowerCase();Object value = https://www.isolves.com/it/cxkf/bk/2021-11-26/rs.getObject(i);map.put(name,value);}list.add(map);}} catch (Exception e) {System.err.println("执行 jdbcUtil.find() 异常...");e.printStackTrace();}return list;}public HashMap
推荐阅读
- 金银花最佳搭配表,搭配喝好
- 迷迭香的副作用和禁忌,迷迭香茶的功效和作用
- 什么样的藏红花是最好的,目前国内市场上的藏红花价格参差不齐
- 世界十大名茶最新排名,贵的十大名茶介绍
- 金线莲养肝茶的作用与功效,金线莲的功效与作用
- 贡菊是白菊花还是黄菊花,解析白菊花与黄菊花的区别
- 旋覆花代赭石汤原文,旋覆花的药用配伍
- 决明子的作用与功效与作用,决明子泡水的功效与作用
- 泡桐花期中蜂群管理,春季管理蜂群的五大重点
- 杭白菊花泡水喝的功效,杭白菊的功效