shiro標籤 與 權限註解

引入shiro標籤
如下:
<%@ taglib prefix=”shiro” uri=”http://shiro.apache.org/tags” %>


<!-- authenticated 用戶已經經過身份驗證,但不是記住我登錄的 -->
<shiro:authenticated>
    <shiro:principal />已經經過身份驗證<br><br>
</shiro:authenticated>

<!-- 用戶沒有進行身份驗證,記住我自動登錄的屬於沒有進行身份驗證 -->
<shiro:notAuthenticated>
    用戶沒有進行身份驗證,記住我自動登錄的屬於沒有進行身份驗證<br><br>
</shiro:notAuthenticated>

<!-- guest  :用戶沒有驗證時顯示相應信息 ,如登錄等相關信息-->
<shiro:guest>
    <a href="login.jsp">登錄</a><br><br>
</shiro:guest>

<!-- 當前用戶有任意一個角色將會顯示body體中的內容 -->
<shiro:hasAnyRoles name="admin,user,manager">
    <shiro:principal></shiro:principal>擁有admin/user/manager中的角色<br><br>
</shiro:hasAnyRoles>

<!-- 當前用戶有相應的權限,將顯示body體中的信息 -->
<shiro:hasPermission name="customer:delete">
    <shiro:principal />擁有customer:delete權限<br><br>
</shiro:hasPermission>

<!-- 當前用戶沒有相應的權限,將顯示body體中的信息 -->
<shiro:lacksPermission name="customer:delete">
    沒有權限customer:delete<br><br>
</shiro:lacksPermission>

<!-- 當前用戶沒有相應的角色,將顯示body中的信息 -->
<shiro:lacksRole name="manager">
    <shiro:principal></shiro:principal>沒有manager角色<br><br>
</shiro:lacksRole>

<!-- user: 用戶已經經過認證/記住我登錄後 顯示相應的信息 -->
<shiro:user>
    <a href="logout">登出</a><br><br>
</shiro:user>

<!-- 當前用戶是否擁有該角色,有就顯示相關信息 -->
<shiro:hasRole name="admin">
    <a href="admin.jsp">Admin Page</a><br><br>
</shiro:hasRole>

shiro註解使用規則:
shiro註解可在 controller 跟 service 中使用

@RequiresAuthentication
表示當前Subject已經通過login 進行了身份驗證,
即 Subject. isAuthenticated() 返回 true

@RequiresUser
表示當前Subject 已經身份驗證 或者 通過記住我登錄 的

@RequiresGuest
表示當前Subject 沒有身份驗證 或 通過記住我登錄過,即是遊客身份

@RequiresRoles(value={“admin”, “user”},
logical= Logical.AND)
表示當前 Subject 需要角色爲 admin 和 user

@RequiresPermissions(value={“user:create”, “user:delete”},
logical= Logical.OR)
表示當前 Subject 需要權限 user:create 或 user:delete

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章