日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Security——基于读写锁的动态权限配置FilterInvocationSecurityMetadataSource实现类

發(fā)布時間:2024/10/5 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Security——基于读写锁的动态权限配置FilterInvocationSecurityMetadataSource实现类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

問題描述

每次都加載資源,效率低下。

解決方案

/*** @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<>();/*** 適用于很多線程從一個數(shù)據(jù)結(jié)構(gòu)讀取數(shù)據(jù)而很少的線程修改其中數(shù)據(jù)*/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();}/*** 加載權(quán)限*/private void loadResourcePermission() {writeLock.lock(); //locks all readers and writerstry{// do write datalog.info("加載權(quán)限");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();}}/*** 權(quán)限重載*/public void resetResourcePermission(){log.info("權(quán)限重載");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 總結(jié)(四) Spring Security 動態(tài)權(quán)限配置

總結(jié)

以上是生活随笔為你收集整理的Spring Security——基于读写锁的动态权限配置FilterInvocationSecurityMetadataSource实现类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。