前言
- sprint boot 2.3.4.RELEASE
说明
shiro 未提供对 thymeleaf 的支持。第三方项目 thymeleaf-extras-shiro 提供了在 thymeleaf 上使用 shiro 标签的能力。
A Thymeleaf dialect for Apache Shiro tags.
thymeleaf-extras-shiro
github
https://github.com/theborakompanioni/thymeleaf-extras-shiro
Maven
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<!-- 目前最新版为2.0.0 -->
<version>${thymeleaf-shiro.version}</version>
</dependency>
示例
<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<title>thymeleaf-extras-shiro</title>
</head>
<body>
<p shiro:guest="">Please <a href="login.html">login</a></p>
<p shiro:authenticated="">
Hello, <span shiro:principal=""></span>, how are you today?
</p>
</body>
</html>
输出用户昵称
<shiro:principal property="nickname" />
还可以这么写:
<th:block shiro:principal="" property="nickname" />
还可以这么写:
<span shiro:principal="" property="nickname" />
标签说明
Apache Shiro tags :
- https://shiro.apache.org/jsp-tag-library.html
- https://github.com/apache/shiro/blob/main/web/src/main/resources/META-INF/shiro.tld
标签 | 含义 |
---|---|
shiro:principal | 当前用户的登录信息,用户名之类 |
shiro:guest=“” | 验证是否是游客,即未认证的用户 |
shiro:user=“” | 验证是否是已认证或已记住用户 |
shiro:authenticated=“” | 验证是否是已认证用户,不包括已记住用户 |
shiro:notAuthenticated= “” | 未认证用户,但是 已记住用户 |
shiro:hasRole=“user1” | 验证当前用户是否属于该角色。 |
shiro:lacksRole=“admin” | 表示没有 admin 角色的用户 |
shiro:hasAllRoles=“admin, user1” | 表示需要同时拥有两种角色 |
shiro:hasAnyRoles=“admin, user1” | 表示 拥有其中一个角色即可 |
shiro:hasPermission="“admin:delete” | 验证当前用户是否拥有指定权限 |
shiro:lacksPermission=“admin:delete” | 类似于 shiro:lacksRole |
shiro:hasAllPermissions=“admin:delete, admin:edit” | 类似于 shiro:hasAllRoles |
shiro:hasAnyPermission=“admin:delete, admin:edit” | 类似于 hasAnyRoles |