【thymeleaf 】在 thymeleaf 中使用 shiro 标签

前言

  • 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 :

标签含义
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值