框架开源地址:

青锋开源架构-springboot2.6.x+vue3-antdesign-vite: 青锋-springboot2.6.x+vue3-antdesign-vite开源架构,实现了系统管理模块、权限控制模块(菜单权限、功能按钮权限、数据权限)、代码生成器(单表、树表)、quartz动态定时器等功能。

权限访问控制讲解

GrantedAuthority接口

Spring Security中有一些概念和实现,例如GrantedAuthority用于获得授权来授权/控制访问权限的接口。

我希望对允许的操作(例如userList或user:del)进行允许,这些操作将允许管理员(具有role ROLE_ADMIN)使用。

通过查看org.springframework.security.core.userdetails.UserDetails了身份验证提供程序引用的DAO中使用的接口,该接口消耗了User(请注意最后的GrantedAuthority):

public User(String username, String password, boolean enabled, boolean accountNonExpired,boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities)

当用户执行登录验证之后,查询对应的权限集合authorities,将权限集合返回到User对象中。

获取用户权限集合

我们可以通过userDetailsService.loadUserByUsername("admin").getAuthorities()获取用户的权限信息,这里的权限信息就是上面讲解的authorities设置的权限信息。

userDetailsService.loadUserByUsername("admin").getAuthorities()

项目中如何使用?

配置权限身份认证

我们在SecurityConfigure中设置权限的身份认证,如下:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailService).passwordEncoder(passwordEncoder);
}

加载用户权限

我们通过查询当前登录用户的权限信息,并将权限信息返回到User中。

 List<GrantedAuthority> grantedAuthorityList = AuthorityUtils.commaSeparatedStringToAuthorityList(permissions);AuthUser authUser = new AuthUser(uPd.get("login_name").toString()+":"+uPd.get("id").toString()+":"+organize_id, uPd.get("login_password").toString(), true, true, true, notLocked,grantedAuthorityList);

AuthUser继承了User接口,我们通过查看User构造方法如下:

详细代码如下:

 @Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {PageData pd = new PageData();if(username.contains(":")){pd.put("login_name",username.split(":")[0]);}else{pd.put("login_name",username);}PageData uPd = userManager.findUserInfo(pd);System.out.println(uPd);if(Verify.verifyIsNotNull(uPd)){System.out.println(uPd.get("status"));if(uPd.get("status").equals("2")){throw new UsernameNotFoundException("账号已休眠,请联系管理员");}else{if(uPd.get("status").equals("0")){//查询当前用户组织pd.put("user_id",uPd.get("id"));PageData orgPd = userManager.findUserOrganizeInfo(pd);String organize_id = "";if(Verify.verifyIsNotNull(orgPd)){organize_id = orgPd.get("organize_id")+"";}//登录成功pd.put("user_id",uPd.get("id"));String permissions = userManager.findUserPermissions(pd);
//                    System.out.println("===================================查询出来数据权限==============================");
//                    System.out.println(permissions);boolean notLocked = false;if (StringUtils.equals("0", uPd.get("status").toString()))notLocked = true;List<GrantedAuthority> grantedAuthorityList = AuthorityUtils.commaSeparatedStringToAuthorityList(permissions);AuthUser authUser = new AuthUser(uPd.get("login_name").toString()+":"+uPd.get("id").toString()+":"+organize_id, uPd.get("login_password").toString(), true, true, true, notLocked,grantedAuthorityList);return transToAuthUser(authUser,uPd);}else if(uPd.getString("status").equals("1")){throw new UsernameNotFoundException("账号已禁用,请联系管理员");}else if(uPd.getString("status").equals("2")){throw new UsernameNotFoundException("账号已休眠,请联系管理员");}}return null;}else{throw new UsernameNotFoundException("登录名称不存在,请重新输入。");}}

权限SQL数据

通过SQL可以看到,数据权限perms的权限是通过父节点菜单的权限标识符:按钮功能权限标识符,比如用户的查看、新增、编辑、删除方法:

user:info
user:add
user:edit
user:del

请求方法增加权限控制

请求方法中,通过增加注解:@PreAuthorize("hasAnyAuthority('xxx:xxx')")进行权限控制:

@PreAuthorize("hasAnyAuthority('xxx:xxx')")

下面以系统用户权限控制方法进行讲解:

  /*** @title listPage* @description 查询数据分页列表* @author Administrator* @updateTime 2022/1/19 0019 23:41*/@GetMapping("/listPage")@PreAuthorize("hasAnyAuthority('user:info')")public MyResponse listPage(QueryRequest queryRequest, Area area) {String userParams = SecurityContextHolder.getContext().getAuthentication().getName();return new MyResponse().data(dataTable);}/*** @title save* @description 保存数据* @author Administrator* @updateTime 2022/1/19 0019 23:41*/@PostMapping@PreAuthorize("hasAnyAuthority('user:add')")public void save(@Valid @RequestBody Area area,HttpServletResponse response) throws Exception {this.writeJson(response,json);}/*** @title update* @description 更新数据* @author Administrator* @updateTime 2022/1/19 0019 23:41*/@PutMapping@PreAuthorize("hasAnyAuthority('user:edit')")public void update(@Valid @RequestBody Area area,HttpServletResponse response) throws Exception {Json json = new Json();this.writeJson(response,json);}/*** @title delete* @description 删除数据* @author Administrator* @updateTime 2022/1/19 0019 23:41*/@DeleteMapping("/{ids}")@PreAuthorize("hasAnyAuthority('user:del')")public void delete(@NotBlank(message = "{required}") @PathVariable String ids,HttpServletResponse response) throws Exception {Json json = new Json();this.writeJson(response,json);}/*** @title updateStatus* @description 更新状态* @author Administrator* @updateTime 2022/1/19 0019 23:41*/@PostMapping("/updateStatus")@PreAuthorize("hasAnyAuthority('user:status')")public void updateStatus(@Valid @RequestBody Area area,HttpServletResponse response) throws Exception {Json json = new Json();this.writeJson(response,json);}

菜单和按钮权限标识设置

看到这里,大家可能会有疑问,上面提到的user:add,用户菜单user和添加按钮add在哪里设置的呢?

青锋开源架构-springboot2.6.x+vue3-antdesign-vite之springsecurity实现访问权限控制相关推荐

  1. 青锋开源架构-springboot2.6.x+vue3-antdesign-vite之搭建IDEA+Maven+JDK+Mysql开发环境

    安装IDEA IDEA下载安装 1)进入 IDEA 官方下载页面,(官网地址为 IntelliJ IDEA: The Capable & Ergonomic Java IDE by JetBr ...

  2. ssm架构 开源项目_6个开源架构项目签出

    ssm架构 开源项目 架构世界的变化没有软件那样快,但是架构师仍在寻找共享创新设计和思想的新方法. 开源架构运动旨在免费提供架构设计,工程图,3D渲染和文档,以根据开源许可将其集成到其他项目中. 它的 ...

  3. 每日新闻:华为获首个微模块产品PUE测试证书;Linux发布Acumos AI开源架构平台;商汤技联手华侨城中学打造智能实验学校...

    关注中国软件网 最新鲜的企业级干货聚集地 今日热点 华为获得全球首个微模块产品PUE测试证书 近日,第四届数据中心基础设施峰会在西安成功召开,会上,TGG(中国)副主席张松和中国信息通信研究院云大所高 ...

  4. 大数据开源架构包括哪些?

    当前,数据在现代化企业经营中是非常重要的资源.很多企业的经营决策.策略和方法都需要对数据进行分析.今天这篇文章主要是对大数据开源架构的相关知识的探讨.一起来看看大数据开源架构都包括了哪些内容. 1.业 ...

  5. 开源架构Fabric、FISCO BCOS(以下简称“BCOS”)、CITA 技术对比

    转自 https://www.coingogo.com/news/41300 联盟链技术哪家强?开源架构Fabric.FISCO BCOS(以下简称"BCOS").CITA 技术对 ...

  6. 深度学习-14:知名的深度学习开源架构和项目

    深度学习-14:知名的深度学习开源架构和项目 深度学习原理与实践(开源图书)-总目录 人工智能artificial intelligence,AI是科技研究中最热门的方向之一.像IBM.谷歌.微软.F ...

  7. 走火入魔.NET快速开发平台架构与老外的开源架构PK与老外一比高低

    很多人都问,走入入魔.NET架构与老外的开源架构比,有什么优缺点?能有老外的架构强大.思路严谨吗? 也不能光是靠自己吹,自己的东西有多好,先说缺点: 1:首先代码功能没老外的强大,水平没老外高,老外写 ...

  8. 基于 vue3.x + vite + element plus,适配手机、平板、pc 的后台开源免费模板库

    介绍 vue-next-admin 基于 vue3.x + CompositionAPI + typescript + vite + element plus + vue-router-next + ...

  9. Apache Ranger、业务背景、现状与需求、大数据安全组件介绍与对别、系统架构及实践、ranger admin、UserSync、plugin、权限模型、权限实现等

    26.2.1业务背景 26.2.1.1现状&&需求 26.2.2大数据安全组件介绍与对比 26.2.2.2 Apache Sentry 26.2.2.3 Apache Ranger 2 ...

最新文章

  1. Linux下文件如果没有权限不能被Apache访问
  2. 201771010126.王燕《面向对象程序设计(Java)》第六周学习总结
  3. 避免使用aireplay-ng指令时出现AP通道不对的方法
  4. python stock query
  5. swiper动态加载数据左右切换失效
  6. ABAP和Java的destination和JNDI
  7. 如何在 ASP.Net Core 中使用 Consul 来存储配置
  8. 【C++】重载赋值运算符
  9. 文本删除空行_Word的空行、空格、页眉线删不了?8秒一次性处理,教你删掉它们...
  10. 进击的PM:作为产品总监,你需要具备什么样的能力?
  11. php如何判断是ajax,php如何判断是ajax
  12. 基于LabVIEW的个性化打地鼠游戏设计
  13. c51单片机学习笔记二
  14. python京东自动下单_京东自动下单脚本
  15. linux 如何安装whl文件,linux安装whl文件
  16. 网页设计中有效的配色
  17. 搭建一个个人网站需要多少钱预算?
  18. [小甲鱼] 零基础入门python第019讲课后测试题及答案:我的地盘听我的
  19. 详解TCP的四报文挥手
  20. http://www.jb51.net/os/Ubuntu/35201.html

热门文章

  1. <整理不易|全网独家|欢迎收藏>《信息资源管理》第4章真题整理,信息系统资源内容管理
  2. [leetcode]41. First Missing Positive
  3. 物理光源:Linearly Transformed Cosines
  4. python初级 奶茶店自助结算系统的基本代码
  5. 小米9搭载 Android,小米9开箱:新一代安卓机皇,硬件配置不输苹果xsmax
  6. 西电2014计算机研究生,西电2014年计算机研究生833考试大纲
  7. 蛇呆在树上,整天无所事事_指南:如何在无所事事的盒子上安装OroCRM
  8. 骑士CMS存储型XSS(攻击管理员)
  9. USB转串口芯片:CH340
  10. FireFox允许复制粘贴