整合 Thymeleaf
欢迎来到我的 ChatGPT 中转站,极具性价比,为付费不方便的朋友提供便利,有需求的可以添加左侧 QQ 二维码,另外,邀请新用户能获取余额哦!最后说一句,那啥:请自觉遵守《生成式人工智能服务管理暂行办法》。
# 添加依赖
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.1.0</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 修改配置类
//用于解析 thymeleaf 中的 shiro:相关属性
@Bean
public ShiroDialect shiroDialect(){
return new ShiroDialect();
}
1
2
3
4
5
2
3
4
5
# 常用属性
<!-- guest 标签 -->
<shiro:guest>
</shiro:guest>
<!-- 用户没有身份验证时显示相应信息,即游客访问信息 -->
<!-- user 标签 -->
<shiro:user>
</shiro:user>
<!-- 用户已经身份验证/记住我登录后显示相应的信息 -->
<!-- authenticated 标签 -->
<shiro:authenticated>
</shiro:authenticated>
<!-- 用户已经身份验证通过,即 Subject.login 登录成功,不是记住我登录的 -->
<!-- notAuthenticated 标签 -->
<shiro:notAuthenticated>
</shiro:notAuthenticated>
<!-- 用户已经身份验证通过,即没有调用 Subject.login 进行登录,包括记住我自动登录的也属于未进行身份验证 -->
<!-- principal 标签 -->
<shiro: principal/>
<shiro:principal property="username"/>
<!-- 相当于((User)Subject.getPrincipals()).getUsername() -->
<!-- lacksPermission 标签 -->
<shiro:lacksPermission name="org:create">
</shiro:lacksPermission>
<!-- 如果当前 Subject 没有权限将显示 body 体内容 -->
<!-- hasRole 标签 -->
<shiro:hasRole name="admin">
</shiro:hasRole>
<!-- 如果当前 Subject 有角色将显示 body 体内容 -->
<!-- hasAnyRoles 标签 -->
<shiro:hasAnyRoles name="admin,user">
</shiro:hasAnyRoles>
<!-- 如果当前 Subject 有任意一个角色(或的关系)将显示 body 体内容 -->
<!-- lacksRole 标签 -->
<shiro:lacksRole name="abc">
</shiro:lacksRole>
<!-- 如果当前 Subject 没有角色将显示 body 体内容 -->
<!-- hasPermission 标签 -->
<shiro:hasPermission name="user:create">
</shiro:hasPermission>
<!-- 如果当前 Subject 有权限将显示 body 体内容 -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Shiro 登录认证后主页面</title>
</head>
<body>
<h1>Shiro 登录认证后主页面</h1>
<br>
登录用户为:<span th:text="${session.user}"></span>
<br>
<a href="/logout">登出</a>
<br>
<a href="/user/roles">测试授权-角色验证</a>
<br>
<a href="/user/permissions">测试授权-权限验证</a>
<br>
<a href="/user/permissions" shiro:hasPermission="user:add">测试授权-权限验证</a>
</body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
上次更新: 2025/04/12, 05:37:39