目录
前言
我本人之前做开源项目微人事时,曾经有个功能就是根据用户对应的权限来加载菜单,不过那个项目是前后端分离的,而这次介绍的是以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.youkuaiyun.com/qq_34579313/article/details/82024058
QQ:707409741