shiro+thymeleaf 整合
SpringBoot中實(shí)現(xiàn)Shiro控制ThymeLeaf界面按鈕級(jí)權(quán)限控制
?
移動(dòng)開發(fā)
## 需求簡(jiǎn)述
在業(yè)績(jī)核算系統(tǒng)中,我們使用了SpringBoot作為項(xiàng)目的整體架構(gòu),使用ThymeLeaf作為前端界面框架,使用Shiro作為我們的權(quán)限控制框架,Shiro作為輕量級(jí)的權(quán)限框架,使用起來非常方便,但是在使用的過程中我發(fā)現(xiàn),Shiro作為頁面級(jí)的權(quán)限控制框架非常好用,它可以注入到Controller中對(duì)頁面級(jí)的訪問權(quán)限做控制,但是如果我們想要實(shí)現(xiàn)頁面中某個(gè)按鈕或者列表的權(quán)限顯示,單純的在Controller中添加注解就顯得捉襟見肘了。
?
## 解決方案
為了解決上述的需求,我們需要引入一個(gè)新的組件---------------Thymeleaf Extras Shiro ,這個(gè)組件的作用就是可以在thymeleaf中使用自定義標(biāo)簽并配合權(quán)限靈活的控制網(wǎng)頁上的組件顯示與否。
他的官方網(wǎng)站是:[Github][]
?
[Github]:?theborakompanioni/thymeleaf-extras-shiro?"thymeleaf-extras-shiro"
?
## 集成方法
首先我們需要在Pom.xml中引入這個(gè)組件的dependency
<dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>2.0.0</version></dependency>?
引入完成后,直接啟動(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里:
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.0.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version></properties>此處設(shè)置完畢后我們才是真正的將thymeleaf-extras-shiro引入進(jìn)來了,之后我們還要在Shiro的Config文件中對(duì)設(shè)置進(jìn)行相應(yīng)的修改:
@Bean(name = "shiroDialect")
?
? ? public ShiroDialect shiroDialect(){
?
? ? return new ShiroDialect();
?
}
添加這段代碼的目的就是為了在thymeleaf中使用shiro的自定義tag。
好了,現(xiàn)在基本上所有使用shiro-tag的條件都具備了,現(xiàn)在給出前端的代碼示例:
<!DOCTYPE html>
?
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="Thymeleaf"
?
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
?
<head>
?
<meta charset="UTF-8" />
?
<title>Insert title here</title>
?
</head>
?
<body>
?
<h3>index</h3>
?
<!-- 驗(yàn)證當(dāng)前用戶是否為“訪客”,即未認(rèn)證(包含未記住)的用戶。 -->
?
<p shiro:guest="">Please <a href="login.html">login</a></p>
?
?
?
?
?
<!-- 認(rèn)證通過或已記住的用戶。 -->
?
<p shiro:user="">
?
Welcome back John! Not John? Click <a href="login.html">here</a> to login.
?
</p>
?
?
?
<!-- 已認(rèn)證通過的用戶。不包含已記住的用戶,這是與user標(biāo)簽的區(qū)別所在。 -->
?
<p shiro:authenticated="">
?
Hello, <span shiro:principal=""></span>, how are you today?
?
</p>
?
<a shiro:authenticated="" href="updateAccount.html">Update your contact information</a>
?
?
?
<!-- 輸出當(dāng)前用戶信息,通常為登錄帳號(hào)信息。 -->
?
<p>Hello, <shiro:principal/>, how are you today?</p>
?
?
?
?
?
<!-- 未認(rèn)證通過用戶,與authenticated標(biāo)簽相對(duì)應(yīng)。與guest標(biāo)簽的區(qū)別是,該標(biāo)簽包含已記住用戶。 -->
?
<p shiro:notAuthenticated="">
?
Please <a href="login.html">login</a> in order to update your credit card information.
?
</p>
?
?
?
<!-- 驗(yàn)證當(dāng)前用戶是否屬于該角色。 -->
?
<a shiro:hasRole="admin" href="admin.html">Administer the system</a><!-- 擁有該角色 -->
?
?
?
<!-- 與hasRole標(biāo)簽邏輯相反,當(dāng)用戶不屬于該角色時(shí)驗(yàn)證通過。 -->
?
<p shiro:lacksRole="developer"><!-- 沒有該角色 -->
?
Sorry, you are not allowed to developer the system.
?
</p>
?
?
?
<!-- 驗(yàn)證當(dāng)前用戶是否屬于以下所有角色。 -->
?
<p shiro:hasAllRoles="developer, 2"><!-- 角色與判斷 -->
?
You are a developer and a admin.
?
</p>
?
?
?
<!-- 驗(yàn)證當(dāng)前用戶是否屬于以下任意一個(gè)角色。 -->
?
<p shiro:hasAnyRoles="admin, vip, developer,1"><!-- 角色或判斷 -->
?
You are a admin, vip, or developer.
?
</p>
?
?
?
<!--驗(yàn)證當(dāng)前用戶是否擁有指定權(quán)限。 -->
?
<a shiro:hasPermission="userInfo:add" href="createUser.html">添加用戶</a><!-- 擁有權(quán)限 -->
?
?
?
<!-- 與hasPermission標(biāo)簽邏輯相反,當(dāng)前用戶沒有制定權(quán)限時(shí),驗(yàn)證通過。 -->
?
<p shiro:lacksPermission="userInfo:del"><!-- 沒有權(quán)限 -->
?
Sorry, you are not allowed to delete user accounts.
?
</p>
?
?
?
<!-- 驗(yàn)證當(dāng)前用戶是否擁有以下所有角色。 -->
?
<p shiro:hasAllPermissions="userInfo:view, userInfo:add"><!-- 權(quán)限與判斷 -->
?
You can see or add users.
?
</p>
?
?
?
<!-- 驗(yàn)證當(dāng)前用戶是否擁有以下任意一個(gè)權(quán)限。 -->
?
<p shiro:hasAnyPermissions="userInfo:view, userInfo:del"><!-- 權(quán)限或判斷 -->
?
You can see or delete users.
?
</p>
?
<a shiro:hasPermission="pp" href="createUser.html">Create a new User</a>
?
</body>
?
</html>
這里注意一個(gè)細(xì)節(jié),在html界面的頂部加一個(gè)tag標(biāo)簽引入:xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
?
<div class="form-group" id="_frBgImageIdDiv" shiro:hasPermission='hms_nav_manager_nav_template_edit_bg_image'><label for="cname" class="col-lg-2 col-sm-2 control-label">模板背景圖</label><div class="layui-input-inline"><select name="frBgImageId" id="_frBgImageId" lay-verify="required" lay-search=""th:name="frBgImageId"><option value="0">請(qǐng)選擇</option><option th:each="item:${picResourceList}" th:value="${item.id}"th:text="${item.picName}"th:selected="${dmsNavigationTemplate.frBgImageId == item.id}"></option></select></div> </div> import org.apache.shiro.authz.annotation.RequiresPermissions;@ScanResource(name = "導(dǎo)航模板背景圖權(quán)限", resKey = HmsNavIndexController.NAV_TEMPLATE_KEY + ResKeyContrants.Button.EDIT + "_bg_image", parentResKey = HmsNavIndexController.NAV_KEY, level = ResKeyContrants.ResLevel.FOUR,icon = ResKeyContrants.ButtonIcon.EDIT, state = ResKeyContrants.PublicState.ENABLE, type = ResKeyContrants.Res.BUTTON_EDIT, sort = 3) @RequiresPermissions(HmsNavIndexController.NAV_TEMPLATE_KEY + ResKeyContrants.Button.EDIT + "_bg_image") public void popNavTemplateBgImage() { ............. }總結(jié)
以上是生活随笔為你收集整理的shiro+thymeleaf 整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 歼20数量破百,面对亚太F35仍有压力
- 下一篇: VUE举例