thymeleaf使用shiro标签
1 必须导入依赖
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
2 在shiroconfig.java文件中配置方言支持(就是将shrio的语法转为tymeleaf的语法)
//配置方言
@Bean
public ShiroDialect getshirodialect() {
return new ShiroDialect();
}
3 在html中添加依赖
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
1 设置不同的登录状态显示不同的标签
需要shiro的拦截器将该页面设置为未登录也可以访问,或者默认就是未登录可访问
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>登录成功</h1>
<shiro:guest>
<div>欢迎游客访问,</div><a href="/">登录</a>
</shiro:guest>
<shiro:user>
已经登录的用户!!
</shiro:user>
</body>
</html>
2.获取当前登录的用户名
<shiro:principal/>
3/如果用户有某个角色就显示对应的字
<shiro:hasRole name="admin">超级管理员</shiro:hasRole>
<shiro:hasRole name="cmanager">仓库管理</shiro:hasRole>
<shiro:hasRole name="xmanager">销售人员</shiro:hasRole>
<shiro:hasRole name="kmanager">客服人员</shiro:hasRole>
<shiro:hasRole name="zmanager">行政人员</shiro:hasRole>
4/用户的角色有某个权限就显示对应的字
<ul>
<shiro:hasPermission name="sys:x:find"><li><a href="#">查找销售记录</a> </li></shiro:hasPermission>
</ul>
总结:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>登录成功</h1>
<shiro:guest>
<div>欢迎游客访问,</div><a href="/">登录</a>
</shiro:guest>
<shiro:user>
已经登录的用户!!
用户名是 <shiro:principal/>
<div>职位是:
<shiro:hasRole name="admin">超级管理员</shiro:hasRole>
<shiro:hasRole name="cmanager">仓库管理</shiro:hasRole>
<shiro:hasRole name="seller">销售人员</shiro:hasRole>
<shiro:hasRole name="kmanager">客服人员</shiro:hasRole>
<shiro:hasRole name="zmanager">行政人员</shiro:hasRole>
</div>
<div>
具有的权限如下:
<shiro:hasPermission name="order-add"><li><a href="#">添加订单</a> </li></shiro:hasPermission>
<shiro:hasPermission name="order-del"><li><a href="#">删除订单</a> </li></shiro:hasPermission>
<shiro:hasPermission name="order-list"><li><a href="#">查询订单</a> </li></shiro:hasPermission>
<shiro:hasPermission name="user_query"><li><a href="#">张三不具有这个权限,所以应该不显示</a> </li></shiro:hasPermission>
</div>
</shiro:user>
</body>
</html>