1. 程式人生 > >SpringSecurity許可權管理系統實戰—一、專案簡介和開發環境準備

SpringSecurity許可權管理系統實戰—一、專案簡介和開發環境準備

## 目錄 [SpringSecurity許可權管理系統實戰—一、專案簡介和開發環境準備](https://blog.csdn.net/HYDCS/article/details/107282166) [SpringSecurity許可權管理系統實戰—二、日誌、介面文件等實現](https://blog.csdn.net/HYDCS/article/details/107284901) [SpringSecurity許可權管理系統實戰—三、主要頁面及介面實現](https://blog.csdn.net/HYDCS/article/details/107342644) [SpringSecurity許可權管理系統實戰—四、整合SpringSecurity(上)](https://blog.csdn.net/HYDCS/article/details/107367064) [SpringSecurity許可權管理系統實戰—五、整合SpringSecurity(下)](https://blog.csdn.net/HYDCS/article/details/107510905) [SpringSecurity許可權管理系統實戰—六、SpringSecurity整合jwt](https://blog.csdn.net/HYDCS/article/details/107732916) [SpringSecurity許可權管理系統實戰—七、處理一些問題](https://blog.csdn.net/HYDCS/article/details/107765898) ## 前言 博主的文筆有些差,大家多擔待 ## 一、簡介 ​ 在企業應用中,認證和授權是非常重要的一部分內容,業界最出名的兩個框架就是大名鼎鼎的 Shiro和Spring Security。本次我選取的是和SpringBoot更好相容的SpringSecurity。 ## 二、什麼是RBAC ​ RBAC是Role Based Access Control的縮寫,是基於角色的訪問控制。一般都是分為使用者(user), 角色(role),許可權(permission)三個實體,角色(role)和許可權(permission)是多對多的 關係,使用者(user)和角色(role)也是多對多的關係。使用者(user)和許可權(permission) 之間沒有直接的關係,都是通過角色作為代理,才能獲取到使用者(user)擁有的許可權。 ​ 以下是RBAC0的模型 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200710134813352.png#pic_center) [詳細解釋見](https://blog.csdn.net/qq_28988969/article/details/100995546) ## 三、系統功能 - 使用者管理:提供使用者的相關配置 - 角色管理:對許可權與選單進行分配 - 選單管理:已實現選單動態路由,後端可配置化,支援多級選單 - 字典管理:可維護常用一些固定的資料 - 系統日誌:記錄使用者操作日誌與異常日誌 - SQL監控:採用druid 監控資料庫訪問效能 - 程式碼生成:高靈活度生成前後端程式碼,減少大量重複的工作任務 - 介面管理:方便統一檢視管理介面 由於本系統是邊開發寫此文件的,所以可能上述的功能在後續的開發中會有刪改。 ## 四、環境搭建 本次系統非前後端分離專案,基於SpringBoot+Layui,後臺模板選用的是[Pear Admin Layui](https://gitee.com/Jmysy/Pear-Admin-Layui) (我目前見過最漂亮的layui後臺模板,放張圖片讓大家感受一下) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200710134859964.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0hZRENT,size_16,color_FFFFFF,t_70#pic_center) 資料庫設計 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200710134918676.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0hZRENT,size_16,color_FFFFFF,t_70#pic_center) 目前先這樣,之後還會擴充套件。 在idea中新建SpringBoot專案,匯入所需依賴 ```xml org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web
org.springframework.boot spring-boot-devtools runtime true io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage
junit-vintage-engine
org.springframework.security spring-security-test test org.springframework spring-web 5.2.7.RELEASE compile
``` 在專案中把架構搭好,建立對應資料庫表的eneity、dao、service、controller,非常簡單不想佔用篇幅。需要注意的就是實體類中的create_time,和update_time,由於這兩個和id是在多張表中都有出現,所以我們可以把它們抽離出來,有需要的實體類直接繼承就可以了 ```java @Data public abstract class BaseEntity implements Serializable { private static final long serialVersionUID = 8925514045582235838L; private ID id; private Date createTime = new Date(); @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime = new Date(); } ``` 再插一嘴,@Data是lambok提供的一個註解,可以用於生成實體類的get和set方法。使用需要匯入maven依賴,在idea中也要安裝相應外掛。[如何安裝使用使用詳見](https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html) 然後將PearAdmin的資源放入templates下 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200710135012283.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0hZRENT,size_16,color_FFFFFF,t_70#pic_center) 那麼接下來我們先把專案執行起來,在index.html中加入thymeleaf的名稱空間,通過thymeleaf的語法重新引入下資源。 ```html
  • 就眠儀式
    個人資訊
    安全配置
    登出登陸
Plus Admin ``` 因為這裡預設的index介面是console1.html,所以console1.html裡的資源和json也要重新引入,這裡就不放出程式碼了。 Pear自帶了一些json資料,這裡我們先用他的,把路徑改成自己專案的。新建一個HelloController,在裡面配置下路由 ```java @Controller public class HelloController { @GetMapping(value = "/console/console1") @ApiOperation(value = "轉發console1請求") public String console1(){ return "console/console1"; } @GetMapping(value = "/system/organization") public String organization(){ return "system/organization"; } @GetMapping(value = "/system/user") public String user(){ return "system/user"; } @GetMapping(value = "/system/role") public String role(){ return "system/role"; } @GetMapping(value = "/system/power") public String power(){ return "system/power"; } @GetMapping(value = "/page/comment") public String comment(){ return "page/comment"; } } ``` 我們啟動專案,看一下效果 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200710135043286.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0hZRENT,size_16,color_FFFFFF,t_70#pic_center) Pear的選單是通過menu.json來動態生成的。之後的這個資料需要後端返回,但是我先用這個假資料。我把我修改過的menu.json貼上來,避免有些同學頁面出不來。 ```json [{ "id": 1, "title": "工作空間", "type": 0, "icon": "layui-icon layui-icon-console", "href": "", "children": [{ "id": 0, "title": "控制後臺", "icon": "layui-icon layui-icon-console", "type": 1, "openType": "_iframe", "href": "console/console1" }] }, { "id": 4, "title": "系統管理", "icon": "layui-icon layui-icon-set-fill", "type": 0, "href": "", "children": [{ "id": 44, "title": "部門管理", "icon": "layui-icon layui-icon-username", "type": 1, "openType": "_iframe", "href": "system/organization" },{ "id": 41, "title": "使用者管理", "icon": "layui-icon layui-icon-username", "type": 1, "openType": "_iframe", "href": "system/user" }, { "id": 42, "title": "角色管理", "icon": "layui-icon layui-icon-user", "type": 1, "openType": "_iframe", "href": "system/role" }, { "id": 43, "title": "許可權管理", "icon": "layui-icon layui-icon-user", "type": 1, "openType": "_iframe", "href": "system/power" } ] }, { "id": 2, "title": "擴充套件元件", "icon": "layui-icon layui-icon-component", "type": 0, "href": "", "children": [ { "id": 22, "title": "進階元件", "icon": "layui-icon layui-icon-face-smile", "type": 0, "href": "view/common/message.html", "children": [ { "id": 225, "title": "卡片列表", "icon": "layui-icon layui-icon-face-smile", "type": 1, "openType": "_iframe", "href": "view/common/senior/card.html" }, { "id": 224, "title": "樹狀結構", "icon": "layui-icon layui-icon-face-smile", "type": 1, "openType": "_iframe", "href": "view/common/senior/dtree.html" } ] } ] }, { "id": 3, "title": "常用頁面", "icon": "layui-icon layui-icon-face-cry", "type": 0, "href": "", "children": [{ "id": 302, "title": "登入頁面", "icon": "layui-icon layui-icon-face-smile", "type": 1, "openType": "_iframe", "href": "login" }, { "id": 303, "title": "留言板", "icon": "layui-icon layui-icon-face-smile", "type": 1, "openType": "_iframe", "href": "page/comment" } ] }, { "id": "error", "title": "錯誤頁面", "icon": "layui-icon layui-icon-auz", "type": 0, "href": "", "children": [{ "id": 403, "title": "403", "icon": "layui-icon layui-icon-face-smile", "type": 1, "openType": "_iframe", "href": "view/error/403.html" }, { "id": 404, "title": "404", "icon": "layui-icon layui-icon-face-cry", "type": 1, "openType": "_iframe", "href": "view/error/404.html" }, { "id": 500, "title": "500", "icon": "layui-icon layui-icon-face-cry", "type": 1, "openType": "_iframe", "href": "view/error/500.html" } ] } ] ``` ## 五、技術棧 將會涉及到的技術棧(待完善) 1、SpringBoot 2、SpringSecurity 3、MyBatis 4、Apache Log4j2 5、JWT 6、Druid 7、Swagger 8、Redis 9、Layui 10、Pear Admin Layui 等 ## 六、說明 **以上原始碼同步在[gitee](https://gitee.com/witmy/my-springsecurity-plus)和[github](https://github.com/witmy/my-springsecurity-pluss)中,如果可以的話,請給我一個star,