shiro+thymeleaf 整合
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() { ............. }總結
以上是生活随笔為你收集整理的shiro+thymeleaf 整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 歼20数量破百,面对亚太F35仍有压力
- 下一篇: VUE举例