Spring Security——基于读写锁的动态权限配置FilterInvocationSecurityMetadataSource实现类
问题描述
每次都加载资源,效率低下。
解决方案
/*** @author ShenTuZhiGang* @version 1.2.0* @date 2020-03-07 21:57*/
@Slf4j
@Component
public class CustomFilterInvocationSecurityMetadataSourceimplements FilterInvocationSecurityMetadataSource {private final IResourceService iResourceService;private final IRoleService iRoleService;private final AntPathMatcher antPathMatcher = new AntPathMatcher();private final Map<RequestMatcher, Collection<ConfigAttribute>> requestMap = new HashMap<>();/*** 适用于很多线程从一个数据结构读取数据而很少的线程修改其中数据*/ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();/*** 该锁可以被多个读操作共用的读锁,但会排斥写操作*/Lock readLock = rwl.readLock();/*** 该锁排斥所有其他的读操作和写操作*/Lock writeLock = rwl.writeLock();public CustomFilterInvocationSecurityMetadataSource(IResourceService iResourceService,IRoleService iRoleService) {this.iResourceService = iResourceService;this.iRoleService = iRoleService;this.loadResourcePermission();}/*** 加载权限*/private void loadResourcePermission() {writeLock.lock(); //locks all readers and writerstry{// do write datalog.info("加载权限");requestMap.clear();List<Resource> resources = iResourceService.list();for (Resource resource : resources) {List<Role> roles = iRoleService.listRoleBySid(resource.getId());String[] roleArr = new String[roles.size()];for (int i = 0; i < roleArr.length; i++) {roleArr[i] = roles.get(i).getName();}requestMap.put(new AntPathRequestMatcher(resource.getPattern()),SecurityConfig.createList(roleArr));}}finally {writeLock.unlock();}}/*** 权限重载*/public void resetResourcePermission(){log.info("权限重载");loadResourcePermission();}@Overridepublic Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {Set<ConfigAttribute> attributes = new HashSet<>();readLock.lock(); //blocks writers onlytry{//there should be data nowfinal HttpServletRequest request = ((FilterInvocation) o).getRequest();for (Map.Entry<RequestMatcher, Collection<ConfigAttribute>> entry : requestMap.entrySet()) {if (entry.getKey().matches(request)) {attributes.addAll(entry.getValue());}}}finally {readLock.unlock();}
// final String requestUrl = ((FilterInvocation) o).getRequestUrl();
// List<Resource> resources = iResourceService.list();
// Set<ConfigAttribute> attributes = new HashSet<>();
// for (Resource resource : resources) {
// if (antPathMatcher.match(resource.getPattern(), requestUrl)) {
// List<Role> roles = iRoleService.listRoleBySid(resource.getId());
// if (roles.size() > 0) {
// String[] roleArr = new String[roles.size()];
// for (int i = 0; i < roleArr.length; i++) {
// roleArr[i] = roles.get(i).getName();
// }
// attributes.addAll(SecurityConfig.createList(roleArr));
// }
// }
// }if (attributes.size() == 0) {attributes.addAll(SecurityConfig.createList("ROLE_LOGIN"));}return attributes;}@Overridepublic Collection<ConfigAttribute> getAllConfigAttributes() {Set<ConfigAttribute> allAttributes = new HashSet<>();readLock.lock(); //blocks writers onlytry{for (Map.Entry<RequestMatcher, Collection<ConfigAttribute>> entry : requestMap.entrySet()) {allAttributes.addAll(entry.getValue());}}finally {readLock.unlock();}return allAttributes;}@Overridepublic boolean supports(Class<?> aClass) {return FilterInvocation.class.isAssignableFrom(aClass);}
}
参考文章
Spring Boot2 总结(四) Spring Security 动态权限配置
Spring Security——基于读写锁的动态权限配置FilterInvocationSecurityMetadataSource实现类相关推荐
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例(转)...
SpringBoot整合mybatis.shiro.redis实现基于数据库的细粒度动态权限管理系统实例 shiro 目录(?)[+] 前言 表结构 maven配置 配置Druid 配置mybatis ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例...
SpringBoot整合mybatis.shiro.redis实现基于数据库的细粒度动态权限管理系统实例 shiro 目录(?)[+] 1.前言 本文主要介绍使用SpringBoot与shiro实现基 ...
- 将Spring Security OAuth2授权服务JWK与Consul 配置中心结合使用
将Spring Security OAuth2授权服务JWK与Consul 配置中心结合使用 概述 在前文中介绍了OAuth2授权服务简单的实现密钥轮换,与其不同,本文将通过Consul实现我们的目的 ...
- 【NET CORE微服务一条龙应用】第三章 认证授权与动态权限配置
[NET CORE微服务一条龙应用]第三章 认证授权与动态权限配置 介绍 系列目录:[NET CORE微服务一条龙应用]开始篇与目录 在微服务的应用中,统一的认证授权是必不可少的组件,本文将介绍微服务 ...
- Spring Security源码分析十三:Spring Security 基于表达式的权限控制
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring I ...
- Spring Security基于角色的权限管理
1.Spring Security 1.1核心领域概念 认证(Authentication):认证是建立主体(principal)的过程. 主体通常是指在应用程序中执行操作的用户.设备或其他系统 授权 ...
- 基于Spring Security 的Java SaaS应用的权限管理
1. 概述 权限管理,一般指根据系统设置的安全规则或者安全策略,用户可以访问而且只能访问自己被授权的资源.资源包括访问的页面,访问的数据等,这在传统的应用系统中比较常见.本文介绍的则是基于Saas系统 ...
- springBoot整合spring security+JWT实现单点登录与权限管理前后端分离
在前一篇文章当中,我们介绍了springBoot整合spring security单体应用版,在这篇文章当中,我将介绍springBoot整合spring secury+JWT实现单点登录与权限管理. ...
- springBoot整合spring security+JWT实现单点登录与权限管理前后端分离--筑基中期
写在前面 在前一篇文章当中,我们介绍了springBoot整合spring security单体应用版,在这篇文章当中,我将介绍springBoot整合spring secury+JWT实现单点登录与 ...
最新文章
- cisco aaa 授权后门测试
- ajax 的理论以及需要的常用参数
- clion配置_Ubuntu16.04系统下用CLion编译器调试ROS代码详细教程
- linux系统开发环境配置
- 开放下载!《大促背后的前端核心业务实践》
- 中职一年级计算机英语课件,中职生一年级英语.doc
- [Windows编程] 通过GetModuleHandleEx 得到函数调用者所在的DLL/EXE 原创陈本峰2009-02
- css 中多种边框的实现小窍门
- python if else格式_【Python基础】流程控制-if..else
- 常用CSS优化总结——网络性能与语法性能建议
- android平台的计时功能,Android实现简单计时器功能
- 大数据分析技术有什么特点
- Java多线程为什么使用while循环来调用wait方法
- GetTickCount函数在linux下的实现
- Vagrant安装CentOS7镜像
- egret给对象涂颜色
- gjt常用命令---chalee
- 网页内嵌的视频添加 vtt 外挂字幕
- 2020北京邮电大学计算机学院复试经验分享
- 鸭鸭来袭--官方下载
热门文章
- css3抽奖转盘,从零制作CSS3抽奖大转盘
- 数据仓库—stg层_手把手教你创建BI数据仓库STG层
- 中科院开源协会镜像站 Android SDK镜像测试发布
- Myeclipse创建Maven项目提示:Creating maven-archetype-webapp has encountered a problem. 解决方法
- html5二次元插件,送你PS一键制作动漫二次元插件,80套背景+50种效果+20款中英字体...
- ps怎么清屏_PS:oracle恢复删除的数据
- sql两个数字之差取最接近的_从零学DAX/Sql/Python030203SQL数据分类汇总续篇
- 八十、归并排序及其分而治之思想
- 看雪Unicorn高级逆向与反混淆学习笔记
- CIKM 2021 | Deep Retrieval:字节跳动深度召回模型论文精读