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 整合的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 歼20数量破百,面对亚太F35仍有压力
- 下一篇: VUE举例