📄 系统按钮权限使用
内部资料,请刷新扫码登录
pigcloud
# 按钮权限控制
在后台菜单管理中给指定菜单添加 按钮节点 需要指定 权限标志
- 例如: sys_file_add、sys_file_del、sys_file_edit
- 若扩展菜单 (非增删改查),则使用 vuex 保存用户的权限信息,然后通过 v-if 判断是否有权限,如果有权限就渲染这个 dom 元素。 例如:ext_btn
# 前端控制
我们以 用户管理页面来讲解
//按钮v-if使用 <el-table-column align="center" label="操作"> <template slot-scope="scope"> <el-button v-if="ext_btn" size="small" type="success" @click="handleUpdate(scope.row)" >扩展 </el-button> </template> </el-table-column>
Copied!
# 后端权限控制
只需要在接口增加
@PreAuthorize("@pms.hasPermission('XXX')")
Copied!
# 原理
通过获取用户菜单列表,和请求的地址和请求方法对比判断有没有权限
public boolean hasPermission(HttpServletRequest request, Authentication authentication) { Object principal = authentication.getPrincipal(); List<SimpleGrantedAuthority> grantedAuthorityList = (List<SimpleGrantedAuthority>) authentication.getAuthorities(); boolean hasPermission = false; if (principal != null) { if (CollectionUtil.isEmpty(grantedAuthorityList)) { return hasPermission; } Set<MenuVo> urls = new HashSet<>(); for (SimpleGrantedAuthority authority : grantedAuthorityList) { urls.addAll(menuService.findMenuByRole(authority.getAuthority())); } for (MenuVo menu : urls) { if (StringUtils.isNotEmpty(menu.getUrl()) && antPathMatcher.match(menu.getUrl(), request.getRequestURI()) && request.getMethod().equalsIgnoreCase(menu.getMethod())) { hasPermission = true; break; } } } return hasPermission; }
Copied!