javascript
springboot 按钮权限验证_SpringBoot中实现Shiro控制ThymeLeaf界面按钮级权限控制
需求簡(jiǎn)述
在業(yè)績(jī)核算系統(tǒng)中,我們使用了SpringBoot作為項(xiàng)目的整體架構(gòu),使用ThymeLeaf作為前端界面框架,使用Shiro作為我們的權(quán)限控制框架,Shiro作為輕量級(jí)的權(quán)限框架,使用起來非常方便,但是在使用的過程中我發(fā)現(xiàn),Shiro作為頁(yè)面級(jí)的權(quán)限控制框架非常好用,它可以注入到Controller中對(duì)頁(yè)面級(jí)的訪問權(quán)限做控制,但是如果我們想要實(shí)現(xiàn)頁(yè)面中某個(gè)按鈕或者列表的權(quán)限顯示,單純的在Controller中添加注解就顯得捉襟見肘了。
解決方案
為了解決上述的需求,我們需要引入一個(gè)新的組件—————Thymeleaf Extras Shiro ,這個(gè)組件的作用就是可以在thymeleaf中使用自定義標(biāo)簽并配合權(quán)限靈活的控制網(wǎng)頁(yè)上的組件顯示與否。
他的官方網(wǎng)站是:Github
集成方法
首先我們需要在Pom.xml中引入這個(gè)組件的dependency
1
2
3
4
5
6
com.github.theborakompanioni
thymeleaf-extras-shiro
2.0.0
引入完成后,直接啟動(dòng)應(yīng)用會(huì)報(bào)錯(cuò),因?yàn)閠hymeleaf-extras-shiro這個(gè)組件需要thymeleaf3.0支持,而Springboot 1.58版本默認(rèn)的thymeleaf的版本是2.X的,所以我們還要將thymeleaf設(shè)置成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
此處設(shè)置完畢后我們才是真正的將thymeleaf-extras-shiro引入進(jìn)來了,之后我們還要在Shiro的Config文件中對(duì)設(shè)置進(jìn)行相應(yīng)的修改:
1
2
3
4@Bean(name = "shiroDialect")
public ShiroDialect shiroDialect(){
return new ShiroDialect();
}
添加這段代碼的目的就是為了在thymeleaf中使用shiro的自定義tag。
好了,現(xiàn)在基本上所有使用shiro-tag的條件都具備了,現(xiàn)在給出前端的代碼示例:
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
這里注意一個(gè)細(xì)節(jié),在html界面的頂部加一個(gè)tag標(biāo)簽引入:xmlns:shiro=”http://www.pollix.at/thymeleaf/shiro“
總結(jié)
以上是生活随笔為你收集整理的springboot 按钮权限验证_SpringBoot中实现Shiro控制ThymeLeaf界面按钮级权限控制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: cmd小游戏编程100例_学宏程序编程,
- 下一篇: c 最大子序列和_最大子序列和暴力法、分
