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

猜你喜欢

转载自www.cnblogs.com/starmoon1994/p/9362313.html