日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

springboot+springsecurity如何實現動態url細粒度權限認證

瀏覽:152日期:2023-03-02 10:16:54

謹記:Url表只儲存受保護的資源,不在表里的資源說明不受保護,任何人都可以訪問

1、MyFilterInvocationSecurityMetadataSource 類判斷該訪問路徑是否被保護

@Component//用于設置受保護資源的權限信息的數據源public class MyFilterInvocationSecurityMetadataSource implementsFilterInvocationSecurityMetadataSource { @Bean public AntPathMatcher getAntPathMatcher(){return new AntPathMatcher(); }@Autowired //獲取數據庫中的保存的url Url表只儲存受保護的資源,不在表里的資源說明不受保護,任何人都可以訪問 private RightsMapper rightsMapper; @Autowired private AntPathMatcher antPathMatcher; @Override /* * @param 被調用的保護資源 * @return 返回能夠訪問該保護資源的角色集合,如果沒有,則應返回空集合。 */ public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {FilterInvocation fi = (FilterInvocation) object;//獲取用戶請求的UrlString url = fi.getRequestUrl();//先到數據庫獲取受權限控制的UrlList<Rights> us = rightsMapper.queryAll();//用于儲存用戶請求的Url能夠訪問的角色Collection<ConfigAttribute> rs=new ArrayList<ConfigAttribute>();for(Rights u:us){ if (u.getUrl() != null) {//逐一判斷用戶請求的Url是否和數據庫中受權限控制的Url有匹配的if (antPathMatcher.match(u.getUrl(), url)) { //如果有則將可以訪問該Url的角色儲存到Collection<ConfigAttribute> rs.add(rightsMapper.queryById(u.getId()));} }}if(rs.size()>0) { return rs;}//沒有匹配到,就說明此資源沒有被控制,所有人都可以訪問,返回null即可,返回null則不會進入之后的decide方法return null; } @Override public Collection<ConfigAttribute> getAllConfigAttributes() {// TODO 自動生成的方法存根return null; } @Override public boolean supports(Class<?> clazz) {// TODO 自動生成的方法存根return FilterInvocation.class.isAssignableFrom(clazz); }}

rights表中的部分內容:

表結構

springboot+springsecurity如何實現動態url細粒度權限認證

內容:

springboot+springsecurity如何實現動態url細粒度權限認證

2、MyAccessDecisionManager 類判斷該用戶是否有權限訪問

@Component//用于設置判斷當前用戶是否可以訪問被保護資源的邏輯public class MyAccessDecisionManager implements AccessDecisionManager { @Override /* * @param 請求該保護資源的用戶對象 * @param 被調用的保護資源 * @param 有權限調用該資源的集合 */ public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {Iterator<ConfigAttribute> ite = configAttributes.iterator();//遍歷configAttributes,查看當前用戶是否有對應的權限訪問該保護資源while (ite.hasNext()) { ConfigAttribute ca = ite.next(); String needRole = ca.getAttribute(); for (GrantedAuthority ga : authentication.getAuthorities()) {if (ga.getAuthority().equals(needRole)) { // 匹配到有對應角色,則允許通過 return;} }}// 該url有配置權限,但是當前登錄用戶沒有匹配到對應權限,則禁止訪問throw new AccessDeniedException('not allow'); } @Override public boolean supports(ConfigAttribute attribute) {return true; } @Override public boolean supports(Class<?> clazz) {return true; }}3、在SecurityConfig 類中配置說明

@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired MyUserDetailsService myUserDetailsService; @Autowired private SendSmsSecurityConfig sendSmsSecurityConfig; @Autowired private MyAccessDecisionManager myAccessDecisionManager; @Autowired private MyFilterInvocationSecurityMetadataSource myFilterInvocationSecurityMetadataSource; //加密機制 @Bean public PasswordEncoder passwordEncoder() {return NoOpPasswordEncoder.getInstance(); // 不加密 } //認證 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//對請求授權.antMatchers('/**').permitAll().anyRequest()//任何請求.authenticated()//登錄后訪問.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() { @Override public <O extends FilterSecurityInterceptor> O postProcess( O fsi) {fsi.setSecurityMetadataSource(myFilterInvocationSecurityMetadataSource);fsi.setAccessDecisionManager(myAccessDecisionManager);return fsi; }}).and().csrf().disable(); }}

配置如下代碼:

springboot+springsecurity如何實現動態url細粒度權限認證

至此完成所有配置!!!

SpringSecurity解決公共接口自定義權限驗證失效問題,和源碼分析背景:

自定義權限認證,一部分接口必須要有相應的角色權限,一部分接口面向所有訪問者,一部分接口任何人都不能訪問。但是在使用 SpringSecurity的過程中發現,框架會將沒有指定角色列表的URL資源直接放行,不做攔截。

用戶登錄認證成功后,攜帶Token訪問URL資源,spring security 根據Token(請求頭Authorization中)來分辨不同用戶。

用戶權限數據源是一個Map:以 URL資源為Key,以有權訪問的Key的角色列表為Value。

使用時發現當一個接口有Key,但是Value為空或null時,spring security 框架自動放行,導致了權限失效問題。

解決方法有兩種:

第一種方法:

默認rejectPublicInvocations為false。

對需要控制權限的URL資源添加標志,以防止roleList為空,跳過了權限驗證.

公共權限設置為null,不進行權限驗證

第二種方法:

配置rejectPublicInvocations為true

此后roleList為空,或者沒有找到URL資源時,都為拒絕訪問

需要控制權限的URL資源,即使對應角色為空,也會進行權限驗證

公共權限設置為所有角色和匿名角色,不進行權限驗證

package org.springframework.security.access.intercept;/** * 對安全對象(訪問請求+用戶主體)攔截的抽象類源碼 */public abstract class AbstractSecurityInterceptor implements InitializingBean, ApplicationEventPublisherAware, MessageSourceAware {// ... 其他方法省略protected InterceptorStatusToken beforeInvocation(Object object) {Assert.notNull(object, 'Object was null');final boolean debug = logger.isDebugEnabled();if (!getSecureObjectClass().isAssignableFrom(object.getClass())) {throw new IllegalArgumentException('Security invocation attempted for object '+ object.getClass().getName()+ ' but AbstractSecurityInterceptor only configured to support secure objects of type: '+ getSecureObjectClass());}// 從權限數據源獲取了當前 <URL資源> 對應的 <角色列表>Collection<ConfigAttribute> attributes = this.obtainSecurityMetadataSource().getAttributes(object);// 框架在此處判斷URL資源對應的角色列表是否為空if (attributes == null || attributes.isEmpty()) {// rejectPublicInvocations默認為false // 可以配置為true,即角色列表為空的時候不進行放行if (rejectPublicInvocations) {throw new IllegalArgumentException('Secure object invocation '+ object+ ' was denied as public invocations are not allowed via this interceptor. '+ 'This indicates a configuration error because the '+ 'rejectPublicInvocations property is set to ’true’');}if (debug) {logger.debug('Public object - authentication not attempted');}publishEvent(new PublicInvocationEvent(object));return null; // no further work post-invocation}if (debug) {logger.debug('Secure object: ' + object + '; Attributes: ' + attributes);}// 如果當前用戶權限對象為nullif (SecurityContextHolder.getContext().getAuthentication() == null) {credentialsNotFound(messages.getMessage('AbstractSecurityInterceptor.authenticationNotFound','An Authentication object was not found in the SecurityContext'),object, attributes);}Authentication authenticated = authenticateIfRequired();// Attempt authorization,此處調用accessDecisionManager 進行鑒權try {this.accessDecisionManager.decide(authenticated, object, attributes);}catch (AccessDeniedException accessDeniedException) {publishEvent(new AuthorizationFailureEvent(object, attributes, authenticated,accessDeniedException));throw accessDeniedException;}if (debug) {logger.debug('Authorization successful');}if (publishAuthorizationSuccess) {publishEvent(new AuthorizedEvent(object, attributes, authenticated));}// Attempt to run as a different user,這里可以另外配置或修改用戶的權限對象,特殊場景使用Authentication runAs = this.runAsManager.buildRunAs(authenticated, object,attributes);if (runAs == null) {if (debug) {logger.debug('RunAsManager did not change Authentication object');}// no further work post-invocationreturn new InterceptorStatusToken(SecurityContextHolder.getContext(), false,attributes, object);}else {if (debug) {logger.debug('Switching to RunAs Authentication: ' + runAs);}SecurityContext origCtx = SecurityContextHolder.getContext();SecurityContextHolder.setContext(SecurityContextHolder.createEmptyContext());SecurityContextHolder.getContext().setAuthentication(runAs);// need to revert to token.Authenticated post-invocationreturn new InterceptorStatusToken(origCtx, true, attributes, object);}}// ... 其他方法略}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
精品国产一区二区三区性色av| 石原莉奈在线亚洲二区| 日韩精品导航| 亚洲精品视频一二三区| 伊人久久亚洲| 日本在线视频一区二区| 婷婷成人av| 日本不卡视频在线| 欧美日本精品| 久久这里只有精品一区二区| 国产一区二区三区黄网站 | 日韩成人精品一区| 欧美日韩亚洲一区三区| 免费在线观看一区| 色综合狠狠操| 精品免费av在线| 不卡中文一二三区| 在线国产精品一区| 久久国产三级| 成人污污视频| 国产在线日韩| 首页国产欧美久久| 国产欧美日韩精品一区二区三区| 久久精品一本| 久久国产中文字幕| 丝袜亚洲另类欧美| 欧美日韩网址| 高清久久精品| 国产国产精品| 日韩国产在线一| 精品欧美视频| 1024精品一区二区三区| 亚洲精品欧美| 久久精品三级| 久久一区二区中文字幕| 日欧美一区二区| 国产精品66| 欧美日韩一区二区三区视频播放| 免费美女久久99| 国产精品麻豆成人av电影艾秋 | 日本免费久久| 国产精品视区| 国产福利一区二区三区在线播放| 日韩国产专区| 亚洲综合激情在线| 精品九九久久| 噜噜噜久久亚洲精品国产品小说| 国产日本久久| 91看片一区| 蜜臀av性久久久久蜜臀aⅴ流畅 | 一本大道色婷婷在线| 久久亚洲美女| 欧美日韩一区二区三区四区在线观看 | 三级小说欧洲区亚洲区| 欧美另类专区| 欧美亚洲国产日韩| 丝袜美腿一区| 亚洲日本久久| 深夜福利视频一区二区| 午夜欧美精品久久久久久久| 国产高清精品二区| 99国产精品视频免费观看一公开| 国产激情欧美| 丝袜亚洲另类欧美| 女生影院久久| 国产日韩高清一区二区三区在线| 韩日一区二区三区| 国产精品啊啊啊| 午夜在线一区二区| 国产传媒在线观看| 91亚洲精品在看在线观看高清| 午夜精品成人av| 国产精品免费99久久久| 亚洲综合日韩| 日韩.com| 欧美日韩一区二区三区不卡视频| 亚洲少妇在线| 美女福利一区二区三区| 亚洲精品无吗| 欧美~级网站不卡| 久久99青青| 日韩在线卡一卡二| 婷婷成人在线| 欧美极品中文字幕| 中文精品电影| 欧美肉体xxxx裸体137大胆| 美女精品久久| 亚洲欧美日本视频在线观看| 亚洲精品一级二级| 亚洲精品少妇| 国产精品视区| 欧美网站在线| 久久国产精品成人免费观看的软件| 久久只有精品| 久久国产日韩欧美精品| 丝袜诱惑制服诱惑色一区在线观看| 群体交乱之放荡娇妻一区二区| 久久精品日韩欧美| 欧美一区成人| 亚洲深深色噜噜狠狠爱网站 | 无码日韩精品一区二区免费| 狂野欧美性猛交xxxx| 91精品丝袜国产高跟在线| 蜜桃视频在线观看一区二区| 欧美日韩国产欧| 午夜国产欧美理论在线播放| 91精品国产调教在线观看| 中国字幕a在线看韩国电影| 嫩草伊人久久精品少妇av杨幂| 日韩福利视频网| 欧美粗暴jizz性欧美20| 国产欧美欧美| 亚洲综合欧美| 九九综合九九| 欧美影院三区| 国内在线观看一区二区三区| 欧美网站在线| 欧美精品黄色| 国产精品三上| aⅴ色国产欧美| 99综合视频| 欧美亚洲国产激情| 久久一区二区中文字幕| 天堂资源在线亚洲| 国产字幕视频一区二区| 激情综合在线| 激情欧美国产欧美| 久久在线免费| 欧美日韩国产综合网| 亚洲欧美伊人| 99在线精品视频在线观看| 国产精品字幕| 亚洲大片在线| 2023国产精品久久久精品双 | 久久一区欧美| 精品伊人久久| 手机在线电影一区| 日本综合字幕| 午夜久久久久| 日韩一区精品字幕| 免费观看在线综合| 日韩精品91亚洲二区在线观看| 日韩av一区二区在线影视| 国产香蕉精品| 国语精品一区| 精品一区二区三区的国产在线观看| 成人精品视频| 啪啪国产精品| 久久国产精品99国产| 日韩手机在线| 久久尤物视频| 国产99久久| 蜜桃一区二区三区在线观看| 日韩不卡手机在线v区| 你懂的亚洲视频| 99精品视频在线| 久久国产精品99国产| 国产三级精品三级在线观看国产| 六月丁香综合在线视频| 日韩理论片av| 91精品久久久久久久久久不卡| 久久性天堂网| 国产精品国产三级在线观看| 在线精品亚洲欧美日韩国产| 欧美另类综合| 国产精品最新| 91精品蜜臀一区二区三区在线| 蘑菇福利视频一区播放| 欧美国产先锋| 国产一区二区三区四区二区| 欧美一级专区| 麻豆视频久久| 欧美日韩少妇| 国产福利资源一区| 国产一区清纯| 亚洲精品美女91| 国产福利片在线观看| 久久xxxx| 国产91在线精品| 午夜一区在线| 福利视频一区| 亚洲欧洲日韩精品在线| 中文在线а√在线8| 深夜福利亚洲| 日韩网站中文字幕| 欧美日韩亚洲一区在线观看| 久久国产亚洲| 国产福利资源一区| 亚洲激情偷拍| 精品欠久久久中文字幕加勒比| 国产一区二区三区精品在线观看| 亚洲欧美日本国产专区一区| 久久99久久人婷婷精品综合| 尤物tv在线精品| 国产精选久久| 免费观看不卡av| 捆绑调教美女网站视频一区| 久久xxxx| 婷婷精品在线观看| 久久精品国产成人一区二区三区|