Shiro之标签

在使用Shiro标签库前,首先需要在JSP引入Shiro标签:

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
  • guest标签 :验证当前用户是否为“访客”,即未认证(包含未记住)的用户
<shiro:guest> 

    Hi there!  Please <a href="login.jsp">Login</a> or <a href="signup.jsp">Signup</a> today! 

</shiro:guest>
  • user标签 :认证通过或已记住的用户
<shiro:user> 

    Welcome back John!  Not John? Click <a href="login.jsp">here<a> to login. 

</shiro:user>
  • authenticated标签 :已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在
<shiro:authenticated> 

    <a href="updateAccount.jsp">Update your contact information</a>. 

</shiro:authenticated>
  • notAuthenticated标签 :未认证通过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户
<shiro:notAuthenticated> 

    Please <a href="login.jsp">login</a> in order to update your credit card information. 

</shiro:notAuthenticated>
  • principal 标签 :输出当前用户信息,通常为登录帐号信息
Hello, <shiro:principal/>, how are you today?
  • hasRole标签 :验证当前用户是否属于该角色
<shiro:hasRole name="administrator"> 

    <a href="admin.jsp">Administer the system</a> 

</shiro:hasRole>
  • lacksRole标签 :与hasRole标签逻辑相反,当用户不属于该角色时验证通过
<shiro:lacksRole name="administrator"> 

    Sorry, you are not allowed to administer the system. 

</shiro:lacksRole>
  • hasAnyRole标签 :验证当前用户是否属于以下任意一个角色
<shiro:hasAnyRoles name="developer, project manager, administrator"> 

    You are either a developer, project manager, or administrator. 

</shiro:hasAnyRoles>
  • hasPermission标签 :验证当前用户是否拥有指定权限
<shiro:hasPermission name="user:create"> 

    <a href="createUser.jsp">Create a new User</a> 

</shiro:hasPermission>
  • lacksPermission标签 :与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过
<shiro:lacksPermission name="user:create"> 

    <a href="createUser.jsp">Create a new User</a> 

</shiro:lacksPermission>
### Shiro 框架中 JSP 标签的使用方法 #### 1. **引入 Shiro 标签库** 在 JSP 页面中使用 Shiro 提供的标签之前,需要先引入 Shiro标签库。通过以下代码声明标签库: ```jsp <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> ``` 此语句的作用是告诉 JSP 引擎加载 Shiro 的自定义标签库,并为其设置前缀 `shiro`[^1]。 #### 2. **常用 Shiro 标签及其功能** ##### (1)`<shiro:hasRole>` 标签 用于检测当前用户是否拥有指定的角色。如果有,则显示标签体内的内容;如果没有,则忽略。 ```jsp <shiro:hasRole name="admin"> <!-- 如果用户有 admin 角色 --> <button>删除</button> </shiro:hasRole> ``` 当页面渲染时,Shiro 自动调用 `doGetAuthorizationInfo` 方法获取用户的授权信息,并据此决定是否渲染按钮[^3]。 ##### (2)`<shiro:lacksRole>` 标签 与 `<shiro:hasRole>` 相反,用于检测当前用户是否缺少指定角色。 ```jsp <shiro:lacksRole name="admin"> <!-- 如果用户没有 admin 角色 --> <p>您无权访问此功能。</p> </shiro:lacksRole> ``` ##### (3)`<shiro:hasPermission>` 标签 用于检测当前用户是否具备某种权限。 ```jsp <shiro:hasPermission name="user:create"> <!-- 如果用户有 user:create 权限 --> <a href="/createUser">创建用户</a> </shiro:hasPermission> ``` ##### (4)`<shiro:lacksPermission>` 标签 与 `<shiro:hasPermission>` 相反,用于检测当前用户是否缺乏某种权限。 ```jsp <shiro:lacksPermission name="user:create"> <!-- 如果用户没有 user:create 权限 --> <p>您无法创建新用户。</p> </shiro:lacksPermission> ``` ##### (5)`<shiro:user>` 和 `<shiro:guest>` - `<shiro:user>`:仅当用户已登录时才显示其内部的内容。 - `<shiro:guest>`:仅当用户未登录时才显示其内部的内容。 ```jsp <shiro:user> <p>欢迎您,<shiro:principal /></p> </shiro:user> <shiro:guest> <p>请<a href="/login">登录</a>以继续。</p> </shiro:guest> ``` #### 3. **注意事项** - 在使用 Shiro 标签时,需确保项目中已经正确配置了 ShiroFilter 并启用了相应的过滤规则[^5]。 - 若页面中有多个 Shiro 标签,每次都会触发 `doGetAuthorizationInfo` 方法来验证权限。因此,在实际开发中应优化该方法的性能,例如加入缓存机制[^3]。 --- ### 示例代码展示 以下是一个综合使用的示例: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Shiro JSP 标签示例</title> </head> <body> <h1>欢迎来到管理系统</h1> <shiro:user> <p>您好:<shiro:principal /></p> </shiro:user> <shiro:guest> <p><a href="/login">点击此处登录</a></p> </shiro:guest> <hr /> <shiro:hasRole name="admin"> <button onclick="location.href='/delete'">删除记录</button> </shiro:hasRole> <shiro:hasPermission name="user:create"> <a href="/createUser">创建新用户</a> </shiro:hasPermission> <shiro:lacksPermission name="user:create"> <p>抱歉,您没有权限创建新用户。</p> </shiro:lacksPermission> </body> </html> ``` --- ### 总结 Shiro 提供了一套简单易用的 JSP 标签库,能够方便地实现界面级别的权限控制。通过这些标签,开发者可以在前端层面屏蔽掉未经授权的操作入口,从而提升用户体验的同时也增强了系统的安全性[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值