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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM