整合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> 新增</a>
<a shiro.hasPermission="sys:role:update" class="btn btn-primary" @click="update"><i class="fa fa-pencil-square-o"></i> 修改</a>
<a shiro.hasPermission="sys:role:delete" class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i> 删除</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"/>