您的位置:首页 > 编程语言 > Java开发

spring boot 与shiro实战(六)thymeleaf和shiro标签整合使用

2019-03-19 10:36 2271 查看

spring boot 与shiro实战(六)thymeleaf和shiro标签整合使用

本篇文章接自上一篇:spring boot 与shiro实战(五)整合实现用户授权
https://blog.csdn.net/jingjingxiazhe/article/details/88638984
源码git地址:https://gitee.com/maximusrj/springboot-shiro.git
内附表的sql语句,以及黑马shiro的视频和源码以及笔记
1.1导入thymeleaf扩展坐标
在pom.xml文件中添加

<!-- thymel对shiro的扩展坐标 -->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>

1.2配置ShiroDialect
在ShiroConfig类里面添加getShiroDialect方法

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

1.3在页面上使用shiro标签
修改test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试Thymeleaf的使用</title>
</head>
<body>
<h3 th:text="${name}"></h3>
<hr/>
<div shiro:hasPermission="user:add">
进入用户添加功能: <a href="add">用户添加</a><br/>
</div>
<div shiro:hasPermission="user:update">
进入用户更新功能: <a href="update">用户更新</a><br/>
</div>
<a href="toLogin">登录</a>
</body>
</html>

最后在pom.xml文件中添加如下的版本控制,不然的话会报java.lang.NoClassDefFoundError的错误:

<properties>
<thymeleaf.version>3.0.3.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.2.0</thymeleaf-layout-dialect.version>
</properties>

最后进行测试,运行启动类,打开登录界面:http://localhost:8080/toLogin

輸入用户名eric 密码123456,则跳转到只显示用户添加权限的界面:


輸入用户名jack 密码123456,则跳转到只显示用户添加更新权限的界面:

项目整体结构:

源码git地址:https://gitee.com/maximusrj/springboot-shiro.git
内附表的sql语句,以及黑马shiro的视频和源码以及笔记

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: