实例:
spring-shiro.xml
1 2 3 | /admin/repairType/index = roles[ "ROLE_ADMIN" ] /admin/user=roles[ "ROLE_ADMIN" ] /admin/complaint/list= roles[ "ROLE_SERVICE,ROLE_ADMIN" ] |
jsp页面:
1 2 3 4 5 6 7 8 9 | < shiro:hasRole name="ROLE_ADMIN"> < li class="user">< a href="${ctx}/admin/user">用户</ a ></ li > </ shiro:hasRole > < shiro:hasAnyRoles name="ROLE_ADMIN,ROLE_SERVICE"> < li class="complaint">< a href="${ctx}/admin/complaint/list">服务</ a ></ li > </ shiro:hasAnyRoles > < shiro:hasRole name="ROLE_ADMIN"> < li class="system">< a href="${ctx}/admin/repairType/index">系统设置</ a ></ li > </ shiro:hasRole > |
在使用Shiro标签库前,首先需要在JSP引入shiro标签:
1 | <%@ taglib prefix= "shiro" uri= "http://shiro.apache.org/tags" %> |
1、介绍Shiro的标签guest标签 :验证当前用户是否为“访客”,即未认证(包含未记住)的用户。
1 2 3 4 5 | <shiro:guest> Hi there! Please <a href= "login.jsp" >Login</a> or <a href= "signup.jsp" >Signup</a> today! </shiro:guest> |
2、user标签 :认证通过或已记住的用户。
1 2 3 4 5 | <shiro:user> Welcome back John! Not John? Click <a href= "login.jsp" >here<a> to login. </shiro:user> |
3、authenticated标签 :已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在。
1 2 3 4 5 | <shiro:authenticated> <a href= "updateAccount.jsp" >Update your contact information</a>. </shiro:authenticated> |
4、notAuthenticated标签 :未认证通过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。
1 2 3 4 5 | <shiro:notAuthenticated> Please <a href= "login.jsp" >login</a> in order to update your credit card information. </shiro:notAuthenticated> |
5、principal 标签 :输出当前用户信息,通常为登录帐号信息。
1 | Hello, <shiro:principal/>, how are you today? |
6、hasRole标签 :验证当前用户是否属于该角色。
1 2 3 4 5 | <shiro:hasRole name= "administrator" > <a href= "admin.jsp" >Administer the system</a> </shiro:hasRole> |
7、lacksRole标签 :与hasRole标签逻辑相反,当用户不属于该角色时验证通过。
1 2 3 4 5 | <shiro:lacksRole name= "administrator" > Sorry, you are not allowed to administer the system. </shiro:lacksRole> |
8、hasAnyRole标签 :验证当前用户是否属于以下任意一个角色。
1 2 3 4 5 | <shiro:hasAnyRoles name= "developer, project manager, administrator" > You are either a developer, project manager, or administrator. </shiro:lacksRole> |
9、hasPermission标签 :验证当前用户是否拥有指定权限。
1 2 3 4 5 | <shiro:hasPermission name= "user:create" > <a href= "createUser.jsp" >Create a new User</a> </shiro:hasPermission> |
10、lacksPermission标签 :与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过。
1 2 3 4 5 | <shiro:hasPermission name= "user:create" > <a href= "createUser.jsp" >Create a new User</a> </shiro:hasPermission> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)