整合Thymeleaf和shiro标签

本文记录了Thymeleaf和Shiro标签的整合使用。首先导入2.0.0版本的引用,避免低版本与项目其他引用冲突;接着进行ShiroConfig配置;最后介绍了在html上使用Shiro标签的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整合Thymeleaf和shiro标签

本文记录Thymeleaf和shiro标签的整合使用。

1、导入引用

本文导入2.0.0版本,使用过程中导入低版本与项目其他引用有冲突。

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

2、配置ShiroConfig

@Configuration
public class ShiroConfig {

    // 配置ShiroDialect:用于thymeleaf和shiro标签配合使用
    @Bean("shiroDialect")
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}

3、html上使用shiro标签

<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">

或者

<html lang = "en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
//作为属性控制
<button  type="button" shiro:authenticated="true" class="btn btn-outline btn-default">
    <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
</button>
<a shiro.hasPermission="sys:role:save" class="btn btn-primary" @click="add"><i class="fa fa-plus"></i>&nbsp;新增</a>
<a shiro.hasPermission="sys:role:update" class="btn btn-primary" @click="update"><i  class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
<a shiro.hasPermission="sys:role:delete" class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
//如果当前Subject没有权限将显示body体内容
<shiro:lacksPermission name="org:create">
<shiro:hasRole name="录入人员">
   <div class="layui-logo">厦门市城市大数据采集平台</div>
</shiro:hasRole>
//相当于((User)Subject.getPrincipals()).getUsername()。
<shiro:principal property="username"/>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值