spring security实现动态配置url权限的两种方法

2023-05-16

缘起

标准的RABC, 权限需要支持动态配置,spring security默认是在代码里约定好权限,真实的业务场景通常需要可以支持动态配置角色访问权限,即在运行时去配置url对应的访问角色。

基于spring security,如何实现这个需求呢?

最简单的方法就是自定义一个Filter去完成权限判断,但这脱离了spring security框架,如何基于spring security优雅的实现呢?

spring security 授权回顾

spring security 通过FilterChainProxy作为注册到web的filter,FilterChainProxy里面一次包含了内置的多个过滤器,我们首先需要了解spring security内置的各种filter:

AliasFilter ClassNamespace Element or Attribute
CHANNEL_FILTERChannelProcessingFilterhttp/intercept-url@requires-channel
SECURITY_CONTEXT_FILTERSecurityContextPersistenceFilterhttp
CONCURRENT_SESSION_FILTERConcurrentSessionFiltersession-management/concurrency-control
HEADERS_FILTERHeaderWriterFilterhttp/headers
CSRF_FILTERCsrfFilterhttp/csrf
LOGOUT_FILTERLogoutFilterhttp/logout
X509_FILTERX509AuthenticationFilterhttp/x509
PRE_AUTH_FILTERAbstractPreAuthenticatedProcessingFilter SubclassesN/A
CAS_FILTERCasAuthenticationFilterN/A
FORM_LOGIN_FILTERUsernamePasswordAuthenticationFilterhttp/form-login
BASIC_AUTH_FILTERBasicAuthenticationFilterhttp/http-basic
SERVLET_API_SUPPORT_FILTERSecurityContextHolderAwareRequestFilterhttp/@servlet-api-provision
JAAS_API_SUPPORT_FILTERJaasApiIntegrationFilterhttp/@jaas-api-provision
REMEMBER_ME_FILTERRememberMeAuthenticationFilterhttp/remember-me
ANONYMOUS_FILTERAnonymousAuthenticationFilterhttp/anonymous
SESSION_MANAGEMENT_FILTERSessionManagementFiltersession-management
EXCEPTION_TRANSLATION_FILTERExceptionTranslationFilterhttp
FILTER_SECURITY_INTERCEPTORFilterSecurityInterceptorhttp
SWITCH_USER_FILTERSwitchUserFilterN/A

最重要的是FilterSecurityInterceptor,该过滤器实现了主要的鉴权逻辑,最核心的代码在这里:

protected InterceptorStatusToken beforeInvocation(Object object) {
    
        // 获取访问URL所需权限
        Collection<ConfigAttribute> attributes = this.obtainSecurityMetadataSource()
                .getAttributes(object);

    
        Authentication authenticated = authenticateIfRequired();

        // 通过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-invocation
            return 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-invocation
            return new InterceptorStatusToken(origCtx, true, attributes, object);
        }
    }

从上面可以看出,要实现动态鉴权,可以从两方面着手:

  • 自定义SecurityMetadataSource,实现从数据库加载ConfigAttribute
  • 另外就是可以自定义accessDecisionManager,官方的UnanimousBased其实足够使用,并且他是基于AccessDecisionVoter来实现权限认证的,因此我们只需要自定义一个AccessDecisionVoter就可以了

下面来看分别如何实现。

自定义AccessDecisionManager

官方的三个AccessDecisionManager都是基于AccessDecisionVoter来实现权限认证的,因此我们只需要自定义一个AccessDecisionVoter就可以了。

自定义主要是实现AccessDecisionVoter接口,我们可以仿照官方的RoleVoter实现一个:


public class RoleBasedVoter implements AccessDecisionVoter<Object> {

    @Override
    public boolean supports(ConfigAttribute attribute) {
        return true;
    }

    @Override
    public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
        if(authentication == null) {
            return ACCESS_DENIED;
        }
        int result = ACCESS_ABSTAIN;
        Collection<? extends GrantedAuthority> authorities = extractAuthorities(authentication);

        for (ConfigAttribute attribute : attributes) {
            if(attribute.getAttribute()==null){
                continue;
            }
            if (this.supports(attribute)) {
                result = ACCESS_DENIED;

                // Attempt to find a matching granted authority
                for (GrantedAuthority authority : authorities) {
                    if (attribute.getAttribute().equals(authority.getAuthority())) {
                        return ACCESS_GRANTED;
                    }
                }
            }
        }

        return result;
    }

    Collection<? extends GrantedAuthority> extractAuthorities(
        Authentication authentication) {
        return authentication.getAuthorities();
    }

    @Override
    public boolean supports(Class clazz) {
        return true;
    }
}

如何加入动态权限呢?

vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes)里的Object object的类型是FilterInvocation,可以通过getRequestUrl获取当前请求的URL:

  FilterInvocation fi = (FilterInvocation) object;
  String url = fi.getRequestUrl();

因此这里扩展空间就大了,可以从DB动态加载,然后判断URL的ConfigAttribute就可以了。

如何使用这个RoleBasedVoter呢?在configure里使用accessDecisionManager方法自定义,我们还是使用官方的UnanimousBased,然后将自定义的RoleBasedVoter加入即可。

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)
            .exceptionHandling()
            .authenticationEntryPoint(problemSupport)
            .accessDeniedHandler(problemSupport)
        .and()
            .csrf()
            .disable()
            .headers()
            .frameOptions()
            .disable()
        .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
            .authorizeRequests()
            // 自定义accessDecisionManager
            .accessDecisionManager(accessDecisionManager())
          
        .and()
            .apply(securityConfigurerAdapter());

    }


    @Bean
    public AccessDecisionManager accessDecisionManager() {
        List<AccessDecisionVoter<? extends Object>> decisionVoters
            = Arrays.asList(
            new WebExpressionVoter(),
            // new RoleVoter(),
            new RoleBasedVoter(),
            new AuthenticatedVoter());
        return new UnanimousBased(decisionVoters);
    }

自定义SecurityMetadataSource

自定义FilterInvocationSecurityMetadataSource只要实现接口即可,在接口里从DB动态加载规则。

为了复用代码里的定义,我们可以将代码里生成的SecurityMetadataSource带上,在构造函数里传入默认的FilterInvocationSecurityMetadataSource。

public class AppFilterInvocationSecurityMetadataSource implements org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource {

    private FilterInvocationSecurityMetadataSource  superMetadataSource;

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    public AppFilterInvocationSecurityMetadataSource(FilterInvocationSecurityMetadataSource expressionBasedFilterInvocationSecurityMetadataSource){
         this.superMetadataSource = expressionBasedFilterInvocationSecurityMetadataSource;

         // TODO 从数据库加载权限配置
    }

    private final AntPathMatcher antPathMatcher = new AntPathMatcher();
    
    // 这里的需要从DB加载
    private final Map<String,String> urlRoleMap = new HashMap<String,String>(){{
        put("/open/**","ROLE_ANONYMOUS");
        put("/health","ROLE_ANONYMOUS");
        put("/restart","ROLE_ADMIN");
        put("/demo","ROLE_USER");
    }};

    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        FilterInvocation fi = (FilterInvocation) object;
        String url = fi.getRequestUrl();

        for(Map.Entry<String,String> entry:urlRoleMap.entrySet()){
            if(antPathMatcher.match(entry.getKey(),url)){
                return SecurityConfig.createList(entry.getValue());
            }
        }

        //  返回代码定义的默认配置
        return superMetadataSource.getAttributes(object);
    }



    @Override
    public boolean supports(Class<?> clazz) {
        return FilterInvocation.class.isAssignableFrom(clazz);
    }
}

怎么使用?和accessDecisionManager不一样,ExpressionUrlAuthorizationConfigurer 并没有提供set方法设置FilterSecurityInterceptorFilterInvocationSecurityMetadataSource,how to do?

发现一个扩展方法withObjectPostProcessor,通过该方法自定义一个处理FilterSecurityInterceptor类型的ObjectPostProcessor就可以修改FilterSecurityInterceptor

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)
            .exceptionHandling()
            .authenticationEntryPoint(problemSupport)
            .accessDeniedHandler(problemSupport)
        .and()
            .csrf()
            .disable()
            .headers()
            .frameOptions()
            .disable()
        .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
            .authorizeRequests()
            // 自定义FilterInvocationSecurityMetadataSource
            .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                @Override
                public <O extends FilterSecurityInterceptor> O postProcess(
                    O fsi) {
                    fsi.setSecurityMetadataSource(mySecurityMetadataSource(fsi.getSecurityMetadataSource()));
                    return fsi;
                }
            })
        .and()
            .apply(securityConfigurerAdapter());

    }


    @Bean
    public AppFilterInvocationSecurityMetadataSource mySecurityMetadataSource(FilterInvocationSecurityMetadataSource filterInvocationSecurityMetadataSource) {
        AppFilterInvocationSecurityMetadataSource securityMetadataSource = new AppFilterInvocationSecurityMetadataSource(filterInvocationSecurityMetadataSource);
        return securityMetadataSource;
}

小结

本文介绍了两种基于spring security实现动态权限的方法,一是自定义accessDecisionManager,二是自定义FilterInvocationSecurityMetadataSource。实际项目里可以根据需要灵活选择。

延伸阅读:

Spring Security 架构与源码分析


作者:Jadepeng
出处:jqpeng的技术记事本--http://www.cnblogs.com/xiaoqi
您的支持是对博主最大的鼓励,感谢您的认真阅读。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

spring security实现动态配置url权限的两种方法 的相关文章

随机推荐

  • c语言怎么做界面菜单_C语言实现的一个交互小程序(三)

    关于C语言如何有效正确的学习 xff0c 我写了一个专栏 xff1a 你可以这样学习C语言 xff0c 有兴趣的小伙伴可以来看看哦 xff0c 希望能对你有收获 xff01 需要本文源代码的可以关注我后私信我 xff01 阅读本文之前 xf
  • pycharm怎么安装gym_windows 10、Python 3.6 环境下安装OpenAI Gym

    OpenAI Gym发布两年以来 xff0c 官方一直没有给出windows版支持 而我只有一台普通的win10台式机 xff0c 之前一直通过虚拟机上安装Ububtu来学习该框架 xff0c 但是无奈电脑太差 xff0c 而且虚拟机下不支
  • linux好用的下载工具,四款linux下的好工具

    原标题 xff1a 四款linux下的好工具 linux下的好工具很多 xff0c 使用得当会极大的提高工作者的效率 下面小编为大家推荐四个比较好用并且实用的工具 xff0c 对这四款工具在centos和ubuntu两个系统下进行相应的安装
  • 用计算机怎么按e,在计算器上e的多少次方怎样按

    e的x次方计算器 自然常数e就是lim 1 43 1 x xlim 1 43 1 x x xff0c x 43 或lim 1 43 z 1 zlim 1 43 z 1 z xff0c z 0 xff0c 其值约为2 71828 xff0c
  • Docker容器

    官网 www docker com github https github com docker docker github io 开源的容器引擎 xff0c 可以让开发者打包应用以及依赖的库 xff0c 然后发布到任何流行的linux发行
  • 宝塔跨服务器传文件的命令,宝塔面板文件移动复制粘贴你所不知道的小技巧!...

    下面是编程之家 jb51 cc 通过网络收集整理的代码片段 编程之家小编现在分享给大家 xff0c 也给大家做个参考 使用宝塔面板的时候 xff0c 同名文件夹覆盖无法正确覆盖 xff0c 会提示指定目录已经存在的 例如不同的目录下都有一个
  • 云服务器如何显示不出来,云服务器安装显示不出来

    云服务器安装显示不出来 内容精选 换一换 GPU加速型云服务器 xff0c 需要安装Tesla驱动和CUDA工具包以实现计算加速功能 使用公共镜像创建的计算加速型 P系列 实例默认已安装特定版本的Tesla驱动 使用私有镜像创建的GPU加速
  • linux tcp 例程,tcpdump 常用例子

    tcpdump 是一个很常用的网络包分析工具 xff0c 可以用来显示通过网络传输到本系统的 TCP IP 以及其他网络的数据包 tcpdump 使用 libpcap 库来抓取网络报 xff0c 这个库在几乎在所有的 Linux Unix
  • 给你两个字符串str1,str2,找出str2在str1中的位置

    如题 题目参考链接 xff1a http blog csdn net hxz qlh article details 14110221 代码来自非原创 1 include lt iostream gt 2 include lt cstdio
  • Win10间歇性卡顿

    Win10间歇性卡顿 1 关闭不必要的服务 xff1a Windows Update Windows Search SuperFetch Background Intelligent Transfer Service 2 卸载电脑管家 xf
  • HTTP协议 (二) 基本认证

    http协议是无状态的 xff0c 浏览器和web服务器之间可以通过cookie来身份识别 桌面应用程序 比如新浪桌面客户端 xff0c skydrive客户端 跟Web服务器之间是如何身份识别呢 xff1f 阅读目录 什么是HTTP基本认
  • 字符串截取函数--C语言(转)

    1 include lt stdio h gt 2 include lt stdlib h gt 3 4 char substring char ch int pos int length 5 6 char pch 61 ch 7 定义一个
  • HTTP, WWW-Authenticate, Authorization 验证授权 | Apache验证 | Python处理

    2019独角兽企业重金招聘Python工程师标准 gt gt gt HTTP验证 有时你访问网页时会碰到这种情况 xff1a 这种方式是基于HTTP协议的一种验证方式 xff0c 这里可以参考HTTP协议的具体解释 xff1a http w
  • 虚拟化(KVM)

    虚拟化介绍 VMware Workstation就是虚拟化 虚拟化简单讲 xff0c 就是把一台物理计算机虚拟成多台逻辑计算机 xff0c 每个逻辑计算机里面可以运行不同的操作系统 xff0c 相互不受影响 xff0c 这样就可以充分利用硬
  • 头文件中的(全局)变量只能放声明,不能定义

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 头文件中的变量只能声明 xff0c 不能定义 xff0c 否则其他多个 c文件包含该头文件 xff0c 出现重复定义 xff0c 导致链接出错 ifndef define
  • 程序启动时的堆栈

    程序启动时 xff0c Linux把4种类型的信息存放到程序堆栈中 xff1a xff08 1 xff09 命令行参数 xff08 包括程序名称 xff09 的数目 xff08 2 xff09 从shell提示符执行的程序的名称 xff08
  • suse linux双网卡双网关配置

    公司有台SUSE LINUX服务器需要用到2个网络 xff0c 一个网段是192的 xff0c 用来上OP管理平台系统 一个是B网络 xff0c 网段是202的 xff0c 用来上外网 我们都知道一台电脑拥有两个网关是不可能的 xff0c
  • 统计学中抽样比例一般占百分之多少

    要具体问题具体分析 一般和要求的误差限 置信区间及总体方差有关系 比如最基本的简单随机抽样 其样本量确定公式就是1 n 61 1 N 43 d 2 u 2 S 2 样本量和误差限成反比 和置信区间及总体方差成正比 请问一堆url怎么算方差
  • emqtt 试用(二)验证 emq 和 mosquito 的共享订阅

    本地订阅 Local Subscription 本地订阅 Local Subscription 只在本节点创建订阅与路由表 xff0c 不会在集群节点间广播全局路由 xff0c 非常适合物联网数据采集应用 使用方式 订阅者在主题 Topic
  • spring security实现动态配置url权限的两种方法

    缘起 标准的RABC 权限需要支持动态配置 xff0c spring security默认是在代码里约定好权限 xff0c 真实的业务场景通常需要可以支持动态配置角色访问权限 xff0c 即在运行时去配置url对应的访问角色 基于sprin