springBoot集成shiro在thymeleaf的js中使用shiro权限标签

springBoot集成shiro在thymeleaf的js中使用shiro权限标签

一、问题描述

项目用 springboot 框架,在前端页面上使用 shiro 标签的时候遇到了 thymeleaf 模板中的 js 脚本里使用 shiro 标签不起作用这个问题。

由于页面表格使用的是 bootstrapTable ,列表中的按钮是在 js 中动态生成的,目前需求是:列表中的按钮要受权限控制。

二、解决方法

首先定义一个全局变量,赋值为 false ;

<script>hasSaveOrUpdatePermission=false</script><!--定义全局变量--><script>hasDeletePermission=false</script>

在页面中进行判断。如果用户有这个权限,那么把该变量设置为 ture ,并在 js 中使用这个变量;

<shiro:hasPermissionname="department:saveOrUpdate"><script>hasSaveOrUpdatePermission=true</script></shiro:hasPermission><shiro:hasPermissionname="department:delete"><script>hasDeletePermission=true</script></shiro:hasPermission>

如果它为 true ,说明有这个权限,那么我就给它显示删除按钮,如果没有,就显示一条"-"

{  title: '操作',  align: 'center',  formatter: function (value, row, index) {  var actions = [];  if (hasSaveOrUpdatePermission) {      actions.push('<aclass="btn btn-success btn-xs"href="javascript:void(0)"onclick="editOp(' + row.id + ')"><iclass="fa fa-edit"></i>编辑</a>');  } else {    actions.push(' - ');  }  if (hasSaveOrUpdatePermission) {      actions.push('<aclass="btn btn-danger btn-xs btn-delete"href="javascript:void(0)"onclick="deleteOp(' + row.id + ')"shiro:hasPermission="department:delete"><iclass="fa fa-remove"></i>删除</a>');  } else {    actions.push(' - ');  }    return actions.join('');  }}

经过测试,问题已成功解决


文章来源:https://www.jianshu.com/p/b3fa5296de26

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容