• 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

  • 相关阅读:
    迭代器,可迭代对象,生成器区别
    七大经典排序算法
    二叉排序树的插入、生成、删除及查找操作
    二分查找(折半查找)
    顺序查找
    二叉树的创建、遍历及应用
    (原创)一些常见小程序(C)
    顺序队列
    二叉树的创建
    Vue开源项目库汇总
  • 原文地址:https://www.cnblogs.com/starmoon1994/p/9362313.html
Copyright © 2020-2023  润新知