Spring Boot Thymeleaf中使用Shiro标签
2026/9/11 14:07:33 网站建设 项目流程

一、前言

在《Spring-Boot-shiro权限控制》中,当用户访问没有权限的资源时,我们采取的做法是跳转到403页面,但在实际项目中更为常见的做法是只显示当前用户拥有访问权限的资源链接。配合Thymeleaf中的Shiro标签可以很简单的实现这个目标。

实际上Thymeleaf官方并没有提供Shiro的标签,我们需要引入第三方实现,地址为https://github.com/theborakompanioni/thymeleaf-extras-shiro。

二、引入thymeleaf-extras-shiro

<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro --><dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>2.0.0</version></dependency>

三、ShiroConfig配置

引入依赖后,需要在ShiroConfig中配置该方言标签:

@BeanpublicShiroDialectshiroDialect(){returnnewShiroDialect();}

四、首页改造

更改index.html,用于测试Shiro标签的使用:

<!DOCTYPEhtml><htmlxmlns:th="http://www.thymeleaf.org"xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"><head><metacharset="UTF-8"><title>首页</title></head><body><p>你好![[${user.userName}]]</p><pshiro:hasRole="admin">你的角色为超级管理员</p><pshiro:hasRole="test">你的角色为测试账户</p><div><ashiro:hasPermission="user:user"th:href="@{/user/list}">获取用户信息</a><ashiro:hasPermission="user:add"th:href="@{/user/add}">新增用户</a><ashiro:hasPermission="user:delete"th:href="@{/user/delete}">删除用户</a></div><ath:href="@{/logout}">注销</a></body></html>

值得注意的是,在html页面中使用Shiro标签需要给html标签添加xmlns:shiro=“http://www.pollix.at/thymeleaf/shiro”。

五、测试

启动项目,使用wno704(角色为admin,具有user:user,user:add,user:delete权限)账户登录:

使用test(角色为test,仅有user:user权限)账户登录:

六、更多标签

6.1 Attribute

<pshiro:anyTag>Goodbye cruel World!</p>

6.2 Element

<shiro:anyTag><p>Hello World!</p></shiro:anyTag>

6.3 The guest tag

<pshiro:guest="">Please<ahref="login.html">Login</a></p>

6.4 The user tag

<pshiro:user="">Welcome back John! Not John? Click<ahref="login.html">here<a>to login.</p>

6.5 The authenticated tag

<ashiro:authenticated=""href="updateAccount.html">Update your contact information</a>

6.6 The notAuthenticated tag

<pshiro:notAuthenticated="">Please<ahref="login.html">login</a>in order to update your credit card information.</p>

6.7 The principal tag

<p>Hello,<spanshiro:principal=""></span>, how are you today?</p>

or

<p>Hello,<shiro:principal/>, how are you today?</p>

6.8 The hasRole tag

<ashiro:hasRole="administrator"href="admin.html">Administer the system</a>

6.9 The lacksRole tag

<pshiro:lacksRole="administrator">Sorry, you are not allowed to administer the system.</p>

6.10 The hasAllRoles tag

<pshiro:hasAllRoles="developer, project manager">You are a developer and a project manager.</p>

6.11 The hasAnyRoles tag

<pshiro:hasAnyRoles="developer, project manager, administrator">You are a developer, project manager, or administrator.</p>

6.12 The hasPermission tag

<ashiro:hasPermission="user:create"href="createUser.html">Create a new User</a>

6.13 The lacksPermission tag

<pshiro:lacksPermission="user:delete">Sorry, you are not allowed to delete user accounts.</p>

6.14 The hasAllPermissions tag

<pshiro:hasAllPermissions="user:create, user:delete">You can create and delete users.</p>

6.15 The hasAnyPermissions tag

<pshiro:hasAnyPermissions="user:create, user:delete">You can create or delete users.</p>

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询