javascript
springboot 按钮权限验证_SpringBoot中实现Shiro控制ThymeLeaf界面按钮级权限控制
需求簡述
在業績核算系統中,我們使用了SpringBoot作為項目的整體架構,使用ThymeLeaf作為前端界面框架,使用Shiro作為我們的權限控制框架,Shiro作為輕量級的權限框架,使用起來非常方便,但是在使用的過程中我發現,Shiro作為頁面級的權限控制框架非常好用,它可以注入到Controller中對頁面級的訪問權限做控制,但是如果我們想要實現頁面中某個按鈕或者列表的權限顯示,單純的在Controller中添加注解就顯得捉襟見肘了。
解決方案
為了解決上述的需求,我們需要引入一個新的組件—————Thymeleaf Extras Shiro ,這個組件的作用就是可以在thymeleaf中使用自定義標簽并配合權限靈活的控制網頁上的組件顯示與否。
他的官方網站是:Github
集成方法
首先我們需要在Pom.xml中引入這個組件的dependency
1
2
3
4
5
6
com.github.theborakompanioni
thymeleaf-extras-shiro
2.0.0
引入完成后,直接啟動應用會報錯,因為thymeleaf-extras-shiro這個組件需要thymeleaf3.0支持,而Springboot 1.58版本默認的thymeleaf的版本是2.X的,所以我們還要將thymeleaf設置成3.0版本,具體代碼如下,還是在pom.xml里:
1
2
3
4
5
6
7
8
UTF-8
UTF-8
1.8
3.0.0.RELEASE
2.0.0
此處設置完畢后我們才是真正的將thymeleaf-extras-shiro引入進來了,之后我們還要在Shiro的Config文件中對設置進行相應的修改:
1
2
3
4@Bean(name = "shiroDialect")
public ShiroDialect shiroDialect(){
return new ShiroDialect();
}
添加這段代碼的目的就是為了在thymeleaf中使用shiro的自定義tag。
好了,現在基本上所有使用shiro-tag的條件都具備了,現在給出前端的代碼示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
Insert title hereindex
Please login
Welcome back John! Not John? Click here to login.
Hello, , how are you today?
Update your contact information
Hello, , how are you today?
Please login in order to update your credit card information.
Administer the system
Sorry, you are not allowed to developer the system.
You are a developer and a admin.
You are a admin, vip, or developer.
添加用戶
Sorry, you are not allowed to delete user accounts.
You can see or add users.
You can see or delete users.
Create a new User
這里注意一個細節,在html界面的頂部加一個tag標簽引入:xmlns:shiro=”http://www.pollix.at/thymeleaf/shiro“
總結
以上是生活随笔為你收集整理的springboot 按钮权限验证_SpringBoot中实现Shiro控制ThymeLeaf界面按钮级权限控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cmd小游戏编程100例_学宏程序编程,
- 下一篇: java ajax 404_java