thymeleaf使用shiro标签

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>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值