Shiro整合Thymeleaf

本文详细介绍了如何整合Shiro与Thymeleaf,实现基于权限的页面显示控制。通过添加依赖、配置ShiroDialect及使用shiro标签,确保登录用户能访问相应权限的页面。
为什么整合?
  • 登录用户后显示相应的页面

1.Shiro整合Thymeleaf,导包

        <!--Shiro整合Thymeleaf-->
        <!-- 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>

2.在ShiroConfig类中也需要配置,并交给Spring管理

    //整合ShiroDialect:用来整合Shiro Thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }

3.html页面判断

  • xmlns:shiro=“http://www.thymeleaf.org/thymeleaf-extras-shiro”(整合shiro标签)
<!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</title>
</head>
<body>
<h1>首页</h1>
<p th:text="${msg}"></p>
<!--从session中判断值-->
<div th:if="${session.loginUser==null}">
    <a th:href="@{/toLogin}">登录</a>
</div>

<div shiro:hasPermission="user:add">
    <a th:href="@{/user/add/}">add</a>
</div>
<div shiro:hasPermission="user:update">
    <a th:href="@{/user/update}">update</a>
</div>

</body>
</html>

4.认证成功后,将User信息放到session,方便前端判断用户是否登录

  Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("loginUser",user);

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值