標籤:require user mis security use asp tag class logo
Shiro支援三種方式的授權:
1.編程式:通過寫if/else授權碼塊完成:
Subject subject = SecurityUtils.getSubject(); if(subject.hasRole(“admin”)) { //有許可權 } else { //無許可權 }
2.註解式:通過在執行的Java方法上放置相應的註解完成:
@RequiresRoles("admin") public void hello() { //有許可權 }
3.JSP/GSP標籤:在JSP/GSP頁面通過相應的標籤完成:
<shiro:hasRole name="admin"> <!— 有許可權 —> </shiro:hasRole>
這裡我們重點講下第三種方式---jsp標籤
1.瞭解一些標籤的定義
<shiro:guest>歡迎遊客訪問,<a href="${pageContext.request.contextPath}/login.jsp">登入</a> </shiro:guest> 使用者沒有身分識別驗證時顯示相應資訊,即遊客訪問資訊。
<shiro:principal type="java.lang.String" property="username"/> 顯示使用者身份資訊
<shiro:user>歡迎[<shiro:principal/>]登入,<a href="${pageContext.request.contextPath}/logout">退出</a> </shiro:user> 使用者已經身分識別驗證/記住我登入後顯示相應的資訊。
<shiro:hasRole name="admin"> 使用者擁有角色admin </shiro:hasRole>
<shiro:hasAnyRoles name="admin,user"> 使用者擁有角色admin或user </shiro:hasAnyRoles>
<shiro:lacksRole name="abc"> 使用者沒有角色abc </shiro:lacksRole>
<shiro:hasPermission name="user:create"> 使用者擁有許可權user:create </shiro:hasPermission>
<shiro:lacksPermission name="org:create"> 使用者沒有許可權org:create </shiro:lacksPermission>
2.想使用Shiro標籤首先引用標籤庫<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
Apache Shiro學習----授權