2535-springsecurity系列--关于授权角色“ROLE”前缀的问题

版本信息


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.14.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>1.5.14.RELEASE</version>
    <!--实际里面spring-security-web的版本是4.2.7-->
</dependency>



问题

//  在userdetails里给用户授权时,需要给定角色名  授权角色
 List<GrantedAuthority> grantedAuthorityList = AuthorityUtils.createAuthorityList("ROLE_ADMIN","ROLE_PM","ROLE_DEV");
 
 
 
  // 配置url授权验证相关
    private void configAuthorizeRequests(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(CustomSecurityProperties.exclusivePaths)
                .permitAll()
                .antMatchers("/admin/**", "/**/delete").hasAnyRole("ADMIN")
                .anyRequest()
                .authenticated();
    }

使用

授权的时候有ROLE前缀,但是做URL的权限配置时,并没有ROLE前缀。

原因

版本是spring-security-core-4.2.7.RELEASE.jar
源码org.springframework.security.access.vote.RoleVoter ,类中定义了一个前缀private String rolePrefix = "ROLE_";,类中的supports方法会拿权限参数和rolePrefix进行匹配,查看是否是以ROLE_开头。


    public boolean supports(ConfigAttribute attribute) {
        if ((attribute.getAttribute() != null)
        //这里在验证前缀
                && attribute.getAttribute().startsWith(getRolePrefix())) {
            return true;
        }
        else {
            return false;
        }
    }


    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 (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;
    }

第77行中会验证授权角色信息是否以前缀开头

(这是一个投票器,会对当前用户角色信息和所访问的资源信息的权限要求进行匹配,给出 -1 0 1 这样的投票值
投票器参考:https://blog.csdn.net/tjyyyangyi/article/details/79413307

官方文档

官方文档 46.3.3 What does "ROLE_" mean and why do I need it on my role names? https://docs.spring.io/spring-security/site/docs/5.0.6.RELEASE/reference/htmlsingle/#appendix-faq-role-prefix

完整项目工程参考

https://github.com/starmoon1994/springsecurity-collection

转载于:https://www.cnblogs.com/starmoon1994/p/9362313.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值