• Springboot + Spring Security 实现前后端分离登录认证及权限控制


    Springboot + Spring Security 实现前后端分离登录认证及权限控制

    前言
    本文主要的功能
    文章目录
    文章正文
    一、准备工作
    1、统一错误码枚举
    2、统一json返回体
    3、返回体构造工具
    4、pom
    5、配置文件
    二、数据库表设计
    建表语句
    初始化表数据语句
    三、Spring Security核心配置:WebSecurityConfig
    四、用户登录认证逻辑:UserDetailsService
    1、创建自定义UserDetailsService
    2、准备service和dao层方法
    (1)根据用户名查询用户信息
    (2)根据用户名查询用户的权限信息
    五、用户密码加密
    六、屏蔽Spring Security默认重定向登录页面以实现前后端分离功能
    1、实现登录成功/失败、登出处理逻辑
    (1)登录成功
    (2)登录失败
    (3)登出
    2、在WebSecurityConfig中的configure(HttpSecurity http)方法中声明
    八、会话管理(登录过时、限制单用户或多用户登录等)
    1、限制登录用户数量
    2、处理账号被挤下线处理逻辑
    3、在WebSecurityConfig中声明
    九、实现基于JDBC的动态权限控制
    1、权限拦截器
    2、安全元数据源FilterInvocationSecurityMetadataSource
    3、访问决策管理器AccessDecisionManager
    4、在WebSecurityConfig中声明
    十、最终的WebSecurityConfig配置
    十一、结束语

    原文中数据库更正如下:

     1 DROP TABLE IF EXISTS `sys_user`;
     2 create table sys_user
     3 (
     4     id                      int auto_increment
     5         primary key,
     6     account                 varchar(32)          not null comment '账号',
     7     user_name               varchar(32)          not null comment '用户名',
     8     password                varchar(64)          null comment '用户密码',
     9     last_login_time         datetime             null comment '上一次登录时间',
    10     enabled                 tinyint(1) default 1 null comment '账号是否可用。默认为1(可用)',
    11     account_non_expired             tinyint(1) default 1 null comment '是否过期。默认为1(没有过期)',
    12     account_non_locked      tinyint(1) default 1 null comment '账号是否锁定。默认为1(没有锁定)',
    13     credentials_non_expired tinyint(1) default 1 null comment '证书(密码)是否过期。默认为1(没有过期)',
    14     create_time             datetime             null comment '创建时间',
    15     update_time             datetime             null comment '修改时间',
    16     create_user             int                  null comment '创建人',
    17     update_user             int                  null comment '修改人'
    18 )
    19     comment '用户表';
    20 DROP TABLE IF EXISTS `sys_role`;
    21 create table sys_role
    22 (
    23     id               int auto_increment comment '主键id'
    24         primary key,
    25     role_code             varchar(32) null comment '管理code',
    26     role_name        varchar(32) null comment '角色名',
    27     role_description varchar(64) null comment '角色说明'
    28 )
    29     comment '用户角色表';
    30 DROP TABLE IF EXISTS `sys_permission`;
    31 create table sys_permission
    32 (
    33     id              int auto_increment comment '主键id'
    34         primary key,
    35     permission_code varchar(32) null comment '权限code',
    36     permission_name varchar(32) null comment '权限名'
    37 )
    38     comment '权限表';
    39 create table sys_role_permission_relation
    40 (
    41     id            int auto_increment comment '主键id'
    42         primary key,
    43     role_id       int null comment '角色id',
    44     permission_id int null comment '权限id'
    45 )
    46  comment '角色-权限关联关系表';
    47 
    48 create table sys_user_role_relation
    49 (
    50     id      int auto_increment comment '主键id'
    51         primary key,
    52     user_id int null comment '用户id',
    53     role_id int null comment '角色id'
    54 )
    55     comment '用户角色关联关系表';
    56 create table sys_request_path
    57 (
    58     id          int auto_increment comment '主键id'
    59         primary key,
    60     url         varchar(64)  not null comment '请求路径',
    61     description varchar(128) null comment '路径描述'
    62 )
    63     comment '请求路径';
    64 create table sys_request_path_permission_relation
    65 (
    66     id            int null comment '主键id',
    67     url_id        int null comment '请求路径id',
    68     permission_id int null comment '权限id'
    69 )
    70     comment '路径权限关联表';
    71 -- 用户
    72 INSERT INTO sys_user (id, account, user_name, password, last_login_time, enabled, account_non_expired, account_non_locked, credentials_non_expired, create_time, update_time, create_user, update_user) VALUES (1, 'user1', '用户1', '$2a$10$47lsFAUlWixWG17Ca3M/r.EPJVIb7Tv26ZaxhzqN65nXVcAhHQM4i', '2019-09-04 20:25:36', 1, 1, 1, 1, '2019-08-29 06:28:36', '2019-09-04 20:25:36', 1, 1);
    73 INSERT INTO sys_user (id, account, user_name, password, last_login_time, enabled, account_non_expired, account_non_locked, credentials_non_expired, create_time, update_time, create_user, update_user) VALUES (2, 'user2', '用户2', '$2a$10$uSLAeON6HWrPbPCtyqPRj.hvZfeM.tiVDZm24/gRqm4opVze1cVvC', '2019-09-05 00:07:12', 1, 1, 1, 1, '2019-08-29 06:29:24', '2019-09-05 00:07:12', 1, 2);
    74 -- 角色
    75 INSERT INTO sys_role (id, role_code, role_name, role_description) VALUES (1, 'admin', '管理员', '管理员,拥有所有权限');
    76 INSERT INTO sys_role (id, role_code, role_name, role_description) VALUES (2, 'user', '普通用户', '普通用户,拥有部分权限');
    77 -- 权限
    78 INSERT INTO sys_permission (id, permission_code, permission_name) VALUES (1, 'create_user', '创建用户');
    79 INSERT INTO sys_permission (id, permission_code, permission_name) VALUES (2, 'query_user', '查看用户');
    80 INSERT INTO sys_permission (id, permission_code, permission_name) VALUES (3, 'delete_user', '删除用户');
    81 INSERT INTO sys_permission (id, permission_code, permission_name) VALUES (4, 'modify_user', '修改用户');
    82 -- 请求路径
    83 INSERT INTO sys_request_path (id, url, description) VALUES (1, '/getUser', '查询用户');
    84 -- 用户角色关联关系
    85 INSERT INTO sys_user_role_relation (id, user_id, role_id) VALUES (1, 1, 1);
    86 INSERT INTO sys_user_role_relation (id, user_id, role_id) VALUES (2, 2, 2);
    87 -- 角色权限关联关系
    88 INSERT INTO sys_role_permission_relation (id, role_id, permission_id) VALUES (1, 1, 1);
    89 INSERT INTO sys_role_permission_relation (id, role_id, permission_id) VALUES (2, 1, 2);
    90 INSERT INTO sys_role_permission_relation (id, role_id, permission_id) VALUES (3, 1, 3);
    91 INSERT INTO sys_role_permission_relation (id, role_id, permission_id) VALUES (4, 1, 4);
    92 INSERT INTO sys_role_permission_relation (id, role_id, permission_id) VALUES (5, 2, 1);
    93 INSERT INTO sys_role_permission_relation (id, role_id, permission_id) VALUES (6, 2, 2);
    94 -- 请求路径权限关联关系
    95 INSERT INTO sys_request_path_permission_relation (id, url_id, permission_id) VALUES (null, 1, 2);



    原文链接:https://blog.csdn.net/I_am_Hutengfei/article/details/100561564

  • 相关阅读:
    5分钟了解为什么学习Go
    jieba gensim 相似度实现
    functiils.lru_cache缩短递归时间
    python深浅copy和赋值
    ajax上传文件 基于jquery form表单上传文件
    form组件类 钩子函数验证
    基于jquery读取input上传的文件内容
    MongonDB指令汇总
    冥想编程
    《如何阅读一本书》读书笔记
  • 原文地址:https://www.cnblogs.com/xiadongqing/p/12996245.html
Copyright © 2020-2023  润新知