Shiro 提供了 JSTL 標簽用于在 JSP/GSP 頁面進行權限控制,如根據登錄用戶顯示相應的頁面按鈕。
**導入標簽庫**
``` xml
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
```
標簽庫定義在 shiro-web.jar 包下的 META-INF/shiro.tld 中定義。
guest 標簽
``` xml
<shiro:guest>
歡迎游客訪問,<a href="${pageContext.request.contextPath}/login.jsp">登錄</a>
</shiro:guest>
```
用戶沒有身份驗證時顯示相應信息,即游客訪問信息。
**user 標簽**
``` xml
<shiro:guest>
歡迎游客訪問,<a href="${pageContext.request.contextPath}/login.jsp">登錄</a>
</shiro:guest>
```
用戶已經身份驗證 / 記住我登錄后顯示相應的信息。
**authenticated 標簽**
``` xml
<shiro:authenticated>
用戶[<shiro:principal/>]已身份驗證通過
</shiro:authenticated>
```
用戶已經身份驗證通過,即 Subject.login 登錄成功,不是記住我登錄的。
**notAuthenticated 標簽**
``` xml
<shiro:notAuthenticated>
未身份驗證(包括記住我)
</shiro:notAuthenticated>
```
用戶已經身份驗證通過,即沒有調用 Subject.login 進行登錄,包括記住我自動登錄的也屬于未進行身份驗證。
**principal 標簽**
`<shiro: principal/>`
顯示用戶身份信息,默認調用 Subject.getPrincipal() 獲取,即 Primary Principal。
`<shiro:principal type="java.lang.String"/>`
相當于 Subject.getPrincipals().oneByType(String.class)。
`<shiro:principal type="java.lang.String"/>`
相當于 Subject.getPrincipals().oneByType(String.class)。
`<shiro:principal property="username"/>`
相當于 ((User)Subject.getPrincipals()).getUsername()。
**hasRole 標簽**
``` xml
<shiro:hasRole name="admin">
用戶[<shiro:principal/>]擁有角色admin<br/>
</shiro:hasRole>
```
如果當前 Subject 有角色將顯示 body 體內容。
**hasAnyRoles 標簽**
``` xml
<shiro:hasAnyRoles name="admin,user">
用戶[<shiro:principal/>]擁有角色admin或user<br/>
</shiro:hasAnyRoles>
```
如果當前 Subject 有任意一個角色(或的關系)將顯示 body 體內容。
**lacksRole 標簽**
``` xml
<shiro:lacksRole name="abc">
用戶[<shiro:principal/>]沒有角色abc<br/>
</shiro:lacksRole>
```
如果當前 Subject 沒有角色將顯示 body 體內容。
**hasPermission 標簽**
``` xml
<shiro:hasPermission name="user:create">
用戶[<shiro:principal/>]擁有權限user:create<br/>
</shiro:hasPermission>
```
如果當前 Subject 有權限將顯示 body 體內容。
**lacksPermission 標簽**
``` xml
<shiro:lacksPermission name="org:create">
用戶[<shiro:principal/>]沒有權限org:create<br/>
</shiro:lacksPermission>
```
如果當前 Subject 沒有權限將顯示 body 體內容。
另外又提供了幾個權限控制相關的標簽:
**導入自定義標簽庫**
`<%@taglib prefix="zhang" tagdir="/WEB-INF/tags" %>`
**示例**
``` xml
<zhang:hasAllRoles name="admin,user">
用戶[<shiro:principal/>]擁有角色admin和user<br/>
</zhang:hasAllRoles>
<zhang:hasAllPermissions name="user:create,user:update">
用戶[<shiro:principal/>]擁有權限user:create和user:update<br/>
</zhang:hasAllPermissions>
<zhang:hasAnyPermissions name="user:create,abc:update">
用戶[<shiro:principal/>]擁有權限user:create或abc:update<br/>
</zhang:hasAnyPermissions>
```
hasAllRoles 表示擁有所有相關的角色;hasAllPermissions 表示擁有所有相關的權限;hasAnyPermissions 表示擁有任意一個相關的權限。
- 第1章 Shiro簡介
- 1.1 簡介
- 第2章 身份驗證
- 第3章 授權
- 第4章 INI配置
- 第5章 編碼 / 加密
- 第6章 Realm及相關對象
- 第7章 與Web集成
- 第8章 攔截器機制
- 第9章 JSP標簽
- 第10章 會話管理
- 第11章 緩存機制
- 第12章 與Spring集成
- 第13章 RememberMe
- 第14章 SSL
- 第15章 單點登錄
- 第16章 綜合實例
- 第17章 OAuth2集成
- 第18章 并發登錄人數控制
- 第19章 動態URL權限控制
- 第20章 無狀態Web應用集成
- 第21章 授予身份及切換身份
- 第22章 集成驗證碼
- 第23章 多項目集中權限管理及分布式會話
- 第24章 在線會話管理