基于shiro的权限查询及权限控制逻辑设计

标准样本表:

  1. grouptable:小组表
  • gid:小组id
  • gname:小组名:
  • gcontext:小组介绍
  1. roletable:角色表
  • gid:小组id
  • rid:角色id
  • rname:角色名
  1. permtable:权限表
  • rid:角色id
  • perm:权限
  • pcontext:权限备注

grouptable
在这里插入图片描述
roletable
在这里插入图片描述
permtable
在这里插入图片描述

用户-权限绑定表:
根据用户查询用户所属项目组,角色,以及权限,根据用户id绑定写入。

  • uid:用户id
  • gname:小组名
  • rname:角色名
  • perms:权限
    在这里插入图片描述
    前端赋权呈现:
    根据样本表,动态刷新下拉参数。
    在这里插入图片描述
    ajax动态刷新下拉参数,例如角色刷新。
    界面代码:
<!--管理权限弹框-->
<div class="cover" id='updateUserPermscover' shiro:hasPermission="user:update" style="display: none; height: auto">
	<div class="addHint">
		<div id="updateUserPermsheader">
			<span style="font-size: 1.75rem;">编辑用户权限</span>
			<div class="header-right" onclick="hideUserPermsUpdate()">x</div><br>
			<div class="displayFrame">
				<h3 id="updateUserPermId"></h3>
				<select id="selectGroup" name="selectGroup" onchange="selectRole()">
					<option>——请选择部门——</option>
				</select>
				<select id="selectRole" name="selectRole" onchange="selectPerms()">
					<option>——请选择角色——</option>
				</select>
				<ul id="selectPerms" name="selectPerms" class="list-group list-group-flush">
				</ul>
				<button onclick="updateUserPerms()" class="btn btn-warning">更新</button>
			</div>
		</div>
	</div>
</div>
<!--弹框End-->

ajax请求编写:

//获取该项目组的全部角色
	function selectRole() {
		var gid = $("#selectGroup").val();
		$.ajax({
			type:"GET",
			url:"/role.do/" + gid,
			async:true,
			success:function (data) {
				var rList = data;
				var rSelect = "<option>——请选择角色——</option>";
				for(var i = 0; i < rList.length; i++){
					rSelect += ("<option value = '"+rList[i].rid+"'>"+rList[i].rname+"</option>");
				}
				$("#selectRole").empty();
				$("#selectRole").append(rSelect);
			}
		})
	}

后台响应:

//    根据项目组获取该项目组的角色
    @RequestMapping(value = "/role.do/{gid}")
    @ResponseBody
    public List<RoleTable> getRole(@PathVariable Integer gid){
        List<RoleTable> roleTableList = roleService.getRoleByGid(gid);
        return roleTableList;
    }

shiro权限认证:
realm授权:

    /**
     * 执行授权逻辑
     * */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权");
        //对资源进行授权
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        Subject subject = SecurityUtils.getSubject();
        User user = (User)subject.getPrincipal();
        //根据id查询当前用户的详细信息
//        User dbUser = userService.findUserById(user.getUid());
        //添加角色授权字符串
        authorizationInfo.addRole(user.getRole());
        //到数据库查询当前用户的授权字符串
//        List<Power> powers = powerService.getUserPowers(user.getPid());
        List<UserPerms> perms = roleService.getPermsByUid(user.getUid());
        System.out.println(perms);
        for (UserPerms perm : perms){
            authorizationInfo.addStringPermission(perm.getPerms());
        }
        return authorizationInfo;
    }

mapper接口:

//    根据用户Id获取该用户对应的权限列表
    List<UserPerms> getUserPermsByUid(Integer uid);

mapper:

<!--    根据用户Id获取该用户对应的权限列表-->
    <select id="getUserPermsByUid" resultType="com.chenzl.springboot.pojo.UserPerms" parameterType="integer">
        select * from user_perms where uid=#{uid};
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值