Shiro学习之Thymeleaf中使用Shiro标签

目录

前言

我本人之前做开源项目微人事时,曾经有个功能就是根据用户对应的权限来加载菜单,不过那个项目是前后端分离的,而这次介绍的是以Thymeleaf为模板引擎,正好Shiro和他有个整合包,可以很轻易的实现这个功能。

引入依赖

<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

ShiroConfig配置

添加标签配置

@Bean
public ShiroDialect shiroDialect() {
    return new ShiroDialect();
}

首页

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

测试

管理员的话,权限完整
在这里插入图片描述
换test
在这里插入图片描述
官方地址:https://github.com/theborakompanioni/thymeleaf-extras-shiro
如果你的网不够快,网上也有很多代码!
https://blog.csdn.net/qq_34579313/article/details/82024058
QQ:707409741

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值