Shiro如何实现权限精确到按钮

本文介绍了如何在基于JSP和Thymeleaf的项目中集成Apache Shiro进行权限控制。首先展示了在JSP页面中导入Shiro标签库并实现简单的权限检查。然后提到了添加Thymeleaf与Shiro的依赖,并展示如何在Thymeleaf模板中使用Shiro标签。通过这些步骤,开发者可以轻松地在页面中实现用户登录状态判断、角色和权限的控制功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.页面环境是基于jsp的

官网地址:http://shiro.apache.org/web.html#Web-JSP%2FGSPTagLibrary

需要在你需要的页面导入标签库:
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

2.上代码

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
状态码:${result.code}
<form action="/login" method="post">
    <input type="text" name="username"/>
    <input type="password" name="password"/>
    <input type="submit" value="登陆">
</form>
<br/>
//只有没登录的时候才能看见
<shiro:guest>
    登陆
</shiro:guest>
<br/>
//登录后才能看见
<shiro:authenticated>
    当前用户为:<shiro:principal property="username"/>
    <a href="/logout">退出</a>
</shiro:authenticated>
<br/>
//只有拥有什么样的角色才能查看
<shiro:hasRole name="admin">
    这个是具有admin角色的用户才能看的见
</shiro:hasRole>
<br/>
//拥有什么样的权限才能查看
<shiro:hasPermission name="user:get">
    这个是具有user:get的权限的用户才能看的见
</shiro:hasPermission>
</body>
</html>
​

3.先添加thymeleaf+shiro的依赖

​
<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

4.页面加入

​
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

5.这就和jsp一样支持shiro了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值