SpringBoot中實現Shiro控制ThymeLeaf界面按鈕級權限控制
移動開發
## 需求簡述
在業績覈算系統中,我們使用了SpringBoot作爲項目的整體架構,使用ThymeLeaf作爲前端界面框架,使用Shiro作爲我們的權限控制框架,Shiro作爲輕量級的權限框架,使用起來非常方便,但是在使用的過程中我發現,Shiro作爲頁面級的權限控制框架非常好用,它可以注入到Controller中對頁面級的訪問權限做控制,但是如果我們想要實現頁面中某個按鈕或者列表的權限顯示,單純的在Controller中添加註解就顯得捉襟見肘了。
## 解決方案
爲了解決上述的需求,我們需要引入一個新的組件---------------Thymeleaf Extras Shiro ,這個組件的作用就是可以在thymeleaf中使用自定義標籤並配合權限靈活的控制網頁上的組件顯示與否。
他的官方網站是:[Github][]
[Github]: theborakompanioni/thymeleaf-extras-shiro "thymeleaf-extras-shiro"
## 集成方法
首先我們需要在Pom.xml中引入這個組件的dependency
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
引入完成後,直接啓動應用會報錯,因爲thymeleaf-extras-shiro這個組件需要thymeleaf3.0支持,而Springboot 1.58版本默認的thymeleaf的版本是2.X的,所以我們還要將thymeleaf設置成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>
此處設置完畢後我們纔是真正的將thymeleaf-extras-shiro引入進來了,之後我們還要在Shiro的Config文件中對設置進行相應的修改:
@Bean(name = "shiroDialect")
public ShiroDialect shiroDialect(){
return new ShiroDialect();
}
添加這段代碼的目的就是爲了在thymeleaf中使用shiro的自定義tag。
好了,現在基本上所有使用shiro-tag的條件都具備了,現在給出前端的代碼示例:
<!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>
<!-- 驗證當前用戶是否爲“訪客”,即未認證(包含未記住)的用戶。 -->
<p shiro:guest="">Please <a href="login.html">login</a></p>
<!-- 認證通過或已記住的用戶。 -->
<p shiro:user="">
Welcome back John! Not John? Click <a href="login.html">here</a> to login.
</p>
<!-- 已認證通過的用戶。不包含已記住的用戶,這是與user標籤的區別所在。 -->
<p shiro:authenticated="">
Hello, <span shiro:principal=""></span>, how are you today?
</p>
<a shiro:authenticated="" href="updateAccount.html">Update your contact information</a>
<!-- 輸出當前用戶信息,通常爲登錄帳號信息。 -->
<p>Hello, <shiro:principal/>, how are you today?</p>
<!-- 未認證通過用戶,與authenticated標籤相對應。與guest標籤的區別是,該標籤包含已記住用戶。 -->
<p shiro:notAuthenticated="">
Please <a href="login.html">login</a> in order to update your credit card information.
</p>
<!-- 驗證當前用戶是否屬於該角色。 -->
<a shiro:hasRole="admin" href="admin.html">Administer the system</a><!-- 擁有該角色 -->
<!-- 與hasRole標籤邏輯相反,當用戶不屬於該角色時驗證通過。 -->
<p shiro:lacksRole="developer"><!-- 沒有該角色 -->
Sorry, you are not allowed to developer the system.
</p>
<!-- 驗證當前用戶是否屬於以下所有角色。 -->
<p shiro:hasAllRoles="developer, 2"><!-- 角色與判斷 -->
You are a developer and a admin.
</p>
<!-- 驗證當前用戶是否屬於以下任意一個角色。 -->
<p shiro:hasAnyRoles="admin, vip, developer,1"><!-- 角色或判斷 -->
You are a admin, vip, or developer.
</p>
<!--驗證當前用戶是否擁有指定權限。 -->
<a shiro:hasPermission="userInfo:add" href="createUser.html">添加用戶</a><!-- 擁有權限 -->
<!-- 與hasPermission標籤邏輯相反,當前用戶沒有制定權限時,驗證通過。 -->
<p shiro:lacksPermission="userInfo:del"><!-- 沒有權限 -->
Sorry, you are not allowed to delete user accounts.
</p>
<!-- 驗證當前用戶是否擁有以下所有角色。 -->
<p shiro:hasAllPermissions="userInfo:view, userInfo:add"><!-- 權限與判斷 -->
You can see or add users.
</p>
<!-- 驗證當前用戶是否擁有以下任意一個權限。 -->
<p shiro:hasAnyPermissions="userInfo:view, userInfo:del"><!-- 權限或判斷 -->
You can see or delete users.
</p>
<a shiro:hasPermission="pp" href="createUser.html">Create a new User</a>
</body>
</html>
這裏注意一個細節,在html界面的頂部加一個tag標籤引入: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">請選擇</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 = "導航模板背景圖權限", 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() {
.............
}