shiro+thymeleaf 整合
SpringBoot中实现Shiro控制ThymeLeaf界面按钮级权限控制
移动开发
## 需求简述
在业绩核算系统中,我们使用了SpringBoot作为项目的整体架构,使用ThymeLeaf作为前端界面框架,使用Shiro作为我们的权限控制框架,Shiro作为轻量级的权限框架,使用起来非常方便,但是在使用的过程中我发现,Shiro作为页面级的权限控制框架非常好用,它可以注入到Controller中对页面级的访问权限做控制,但是如果我们想要实现页面中某个按钮或者列表的权限显示,单纯的在Controller中添加注解就显得捉襟见肘了。
## 解决方案
为了解决上述的需求,我们需要引入一个新的组件---------------Thymeleaf Extras Shiro ,这个组件的作用就是可以在thymeleaf中使用自定义标签并配合权限灵活的控制网页上的组件显示与否。
他的官方网站是:[Github][]
[Github]: theborakompanioni/thymeleaf-extras-shiro "thymeleaf-extras-shiro"
## 集成方法
首先我们需要在Pom.xml中引入这个组件的dependency
<dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>2.0.0</version></dependency>
引入完成后,直接启动应用会报错,因为thymeleaf-extras-shiro这个组件需要thymeleaf3.0支持,而Springboot 1.58版本默认的thymeleaf的版本是2.X的,所以我们还要将thymeleaf设置成3.0版本,具体代码如下,还是在pom.xml里:
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.0.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version></properties>
此处设置完毕后我们才是真正的将thymeleaf-extras-shiro引入进来了,之后我们还要在Shiro的Config文件中对设置进行相应的修改:
@Bean(name = "shiroDialect")
public ShiroDialect shiroDialect(){
return new ShiroDialect();
}
添加这段代码的目的就是为了在thymeleaf中使用shiro的自定义tag。
好了,现在基本上所有使用shiro-tag的条件都具备了,现在给出前端的代码示例:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="Thymeleaf"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h3>index</h3>
<!-- 验证当前用户是否为“访客”,即未认证(包含未记住)的用户。 -->
<p shiro:guest="">Please <a href="login.html">login</a></p>
<!-- 认证通过或已记住的用户。 -->
<p shiro:user="">
Welcome back John! Not John? Click <a href="login.html">here</a> to login.
</p>
<!-- 已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在。 -->
<p shiro:authenticated="">
Hello, <span shiro:principal=""></span>, how are you today?
</p>
<a shiro:authenticated="" href="updateAccount.html">Update your contact information</a>
<!-- 输出当前用户信息,通常为登录帐号信息。 -->
<p>Hello, <shiro:principal/>, how are you today?</p>
<!-- 未认证通过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。 -->
<p shiro:notAuthenticated="">
Please <a href="login.html">login</a> in order to update your credit card information.
</p>
<!-- 验证当前用户是否属于该角色。 -->
<a shiro:hasRole="admin" href="admin.html">Administer the system</a><!-- 拥有该角色 -->
<!-- 与hasRole标签逻辑相反,当用户不属于该角色时验证通过。 -->
<p shiro:lacksRole="developer"><!-- 没有该角色 -->
Sorry, you are not allowed to developer the system.
</p>
<!-- 验证当前用户是否属于以下所有角色。 -->
<p shiro:hasAllRoles="developer, 2"><!-- 角色与判断 -->
You are a developer and a admin.
</p>
<!-- 验证当前用户是否属于以下任意一个角色。 -->
<p shiro:hasAnyRoles="admin, vip, developer,1"><!-- 角色或判断 -->
You are a admin, vip, or developer.
</p>
<!--验证当前用户是否拥有指定权限。 -->
<a shiro:hasPermission="userInfo:add" href="createUser.html">添加用户</a><!-- 拥有权限 -->
<!-- 与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过。 -->
<p shiro:lacksPermission="userInfo:del"><!-- 没有权限 -->
Sorry, you are not allowed to delete user accounts.
</p>
<!-- 验证当前用户是否拥有以下所有角色。 -->
<p shiro:hasAllPermissions="userInfo:view, userInfo:add"><!-- 权限与判断 -->
You can see or add users.
</p>
<!-- 验证当前用户是否拥有以下任意一个权限。 -->
<p shiro:hasAnyPermissions="userInfo:view, userInfo:del"><!-- 权限或判断 -->
You can see or delete users.
</p>
<a shiro:hasPermission="pp" href="createUser.html">Create a new User</a>
</body>
</html>
这里注意一个细节,在html界面的顶部加一个tag标签引入:xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
<div class="form-group" id="_frBgImageIdDiv" shiro:hasPermission='hms_nav_manager_nav_template_edit_bg_image'><label for="cname" class="col-lg-2 col-sm-2 control-label">模板背景图</label><div class="layui-input-inline"><select name="frBgImageId" id="_frBgImageId" lay-verify="required" lay-search=""th:name="frBgImageId"><option value="0">请选择</option><option th:each="item:${picResourceList}" th:value="${item.id}"th:text="${item.picName}"th:selected="${dmsNavigationTemplate.frBgImageId == item.id}"></option></select></div>
</div>
import org.apache.shiro.authz.annotation.RequiresPermissions;@ScanResource(name = "导航模板背景图权限", resKey = HmsNavIndexController.NAV_TEMPLATE_KEY + ResKeyContrants.Button.EDIT + "_bg_image", parentResKey = HmsNavIndexController.NAV_KEY, level = ResKeyContrants.ResLevel.FOUR,icon = ResKeyContrants.ButtonIcon.EDIT, state = ResKeyContrants.PublicState.ENABLE, type = ResKeyContrants.Res.BUTTON_EDIT, sort = 3)
@RequiresPermissions(HmsNavIndexController.NAV_TEMPLATE_KEY + ResKeyContrants.Button.EDIT + "_bg_image")
public void popNavTemplateBgImage() {
.............
}
shiro+thymeleaf 整合相关推荐
- SpringBoot——整合数据库,springSecurity,shiro、整合thymeleaf
一.springboot--JDBC 狂神说SpringBoot07:整合JDBC 1.引入web.JDBC API.mysql Driver <dependency><groupI ...
- Shiro权限管理,Shrio与thymeleaf整合
Shiro 核心API: Shiro内置过滤器: Shiro配置文件: Realm实现(授权与身份认证): shiro登录(controller): shiro与thymeleaf整合: 核心API: ...
- cas4.2.7与shiro进行整合
准备工作 cas单点登录开始前准备,请参考cas4.2.7实现单点登录. 与shiro进行整合 注:准备工作的基础上,对cas客户端进行如下改进. 引入相关jar包 shiro-cas-1.2.6.j ...
- 教你 Shiro + SpringBoot 整合 JWT
本篇文章将教大家在 shiro + springBoot 的基础上整合 JWT (JSON Web Token) 如果对 shiro 如何整合 springBoot 还不了解的可以先去看我的上一篇文章 ...
- Shiro教程,整合SpringBoot项目实战(笔记)
1.shiro 1.1什么是权限管理 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己 ...
- SpringMVC与Shiro快速整合
SpringMVC与Shiro快速整合: 好久没有重新整合Shiro框架了,为了方便以后参考查阅,特此将步骤分享出来,共同学习. 一.配置XML文件: 1.到相应的pom.xml中添加shiro相关依 ...
- 视频教程-基于springboot2.x+layui+shiro+redis整合前后端分离的权限管理系统-Java
基于springboot2.x+layui+shiro+redis整合前后端分离的权限管理系统 拥有八年的Java项目开发经验,擅长Java.vue.SpringBoot.springCloud.sp ...
- 【SpringBoot】2021终极版shiro+jwt整合策略,包含shiro1.5+新特性,极简配置,全网独家。
2021终极版shiro+jwt整合策略,包含shiro1.5+新特性,极简配置,全网独家. 前言:shiro1.4的配置之繁琐业内闻名,其实它自1.5之后就有了不小的改进,能够大大精简我们前期的整合 ...
- SpringBoot 整合 Shiro Thymeleaf Mysql 动态授权
文章目录 需求安排 一.前期准备 1. maven依赖 2. 创建数据库+初始化表数据 3. 实体类 4. mapper接口 5. mapper接口映射文件 6. service接口 7. servi ...
最新文章
- 如何把数据在A表空间里面,把它IMP进B表空间
- 批量下载,多文件压缩打包zip下载
- 手动建立makefile简单实例解析
- 打家劫舍(Leetcode)动态规划c语言
- 转: JavaScript判断浏览器类型及版本
- C++_homework_StackSort
- java注释指导手册
- SylixOS 启动时mmu 初始化
- 手机WIFI的上行下行速度不同
- Python学习总结之一 -- 基础篇
- xml文件的概述与应用场景
- SwiftUI Core ML 基础教程
- R9000P2021版拯救者 装ubuntu系统相关问题(WiFi、蓝牙、亮度调节,驱动安装)记录总结
- 线上故障之-内存问题
- Vision Transformer | CVPR 2022 - Beyond Fixation: Dynamic Window Visual Transformer
- OpenJ_Bailian - 3164 奇偶排序
- 华为天猫官方旗舰店粉丝突破一千万
- 第三章 C语言运算符与表达式练习题
- JavaSE有关String的一些API:charAt、indexOf、length、replaceAll、split、subString、trim、toUpLocase、valueOf......
- 一个ABAP中级开发工程师应该学习什么
热门文章
- Python黑客编程基础3网络数据监听和过滤
- Editplus快捷键大全
- 使用jvisualvm.exe 的Btrace插件监控应用程序
- SQL Server 2008最直观的变化上(三)
- ubuntu安装lrzsz报错“E: Unable to locate package”
- 咋样回复计算机桌面删除的东西,被删除的文件怎么恢复 四个妙招巧解决【图解】...
- 怎么在windows安装python模块_如何在windows环境下安装python里的模块
- Microsoft Office PowerPoint 2020中文版
- 浅谈promise用es5实现
- python抓取头条文章