标准样本表:
- grouptable:小组表
- gid:小组id
- gname:小组名:
- gcontext:小组介绍
- roletable:角色表
- gid:小组id
- rid:角色id
- rname:角色名
- 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>