首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【shiro基础】springboot + shiro整合thymeleaf

【shiro基础】springboot + shiro整合thymeleaf

作者头像
master336
发布2026-06-15 19:45:42
发布2026-06-15 19:45:42
600
举报
目录

  • 写在前面
  • 引入依赖
  • 配置方言
  • 页面引入
    • 页面样例
    • 使用说明

写在前面

本文基于【shiro基础】springboot整合shiro 进行整合。

引入依赖

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>2.6.3</version>
  </dependency>
  <dependency>
      <groupId>com.github.theborakompanioni</groupId>
      <artifactId>thymeleaf-extras-shiro</artifactId>
      <version>2.1.0</version>
  </dependency>

配置方言

配置方言,使shiro能在thymeleaf中引入使用

代码语言:javascript
复制
    /**
     * 整合thymeleaf
     */
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

页面引入

分别引入thymeleaf及shiro,注意保持标签与声明的命名空间一致。

页面样例

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>

<div th:if="${session.user == null}">
    <h2>登录</h2>
    <form th:action="@{/auth/sublogin}">

        <input name="userName">
        <input name="password" type="password">
        <input type="submit" value="登录">
    </form>
</div>
<hr>
<div th:if="${session.user != null}">
    <h2 th:text="${session.user.username}"></h2>
    <a th:href="@{/auth/logout}">退出登录</a> &nbsp;
    <div shiro:hasPermission="demo:p1">
        <a th:href="@{/demo/p1}">p1</a> &nbsp;
    </div>
    <div shiro:hasPermission="demo:p2">
        <a th:href="@{/demo/p2}">p2</a>
    </div>
</div>
</body>
</html>

使用说明

使用示例

说明

<shiro:authenticated>

已登录

<shiro:notAuthenticated>

未登录

<shiro:guest>

用户在没有RememberMe时

<shiro:user>

用户在RememberMe时

<shiro:hasAnyRoles name=“abc,123” >

在有abc或者123角色时

<shiro:hasRole name=“abc”>

拥有角色abc

<shiro:lacksRole name=“abc”>

没有角色abc

<shiro:hasPermission name=“abc”>

拥有权限资源abc

<shiro:lacksPermission name=“abc”>

没有abc权限资源

<shiro:principal>

显示用户身份名称

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • 写在前面
  • 引入依赖
  • 配置方言
  • 页面引入
    • 页面样例
    • 使用说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档