2011-10-16 16 views
2

让所有的角色都配置角色和工作的层次结构:我如何从RoleHierarchyImpl

<beans:bean id="roleHierarchy" 
    class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl"> 
    <beans:property name="hierarchy"> 
     <beans:value> 
      ROLE_ADMIN > ROLE_PRIVILEGED 
      ROLE_PRIVILEGED > ROLE_USER 
      ROLE_USER > ROLE_ANONYMOUS 
     </beans:value> 
    </beans:property> 
</beans:bean> 

对于用户角色设定我需要访问我已经定义了哪些角色。我怎样才能实现它?可能与roleHierarchy.getReachableGrantedAuthorities,但我不知道,什么把它作为参数。提前致谢。

回答

3

据我所知,你想从一个给定的授权机构获得所有可以授权的机构。如果是这种情况,下面是解决办法的解决方案:

  • 首先从春天ApplicationContext

    ApplicationContext context = new FileSystemXmlApplicationContext(
         "--path--"); 
    BeanFactory factory = context; 
    RoleHierarchyImpl roleHierarchy = (RoleHierarchyImpl) factory.getBean("roleHierarchy");` 
    

得到RoleHierarchyImpl实例之一或创建一个新的实例,并加载层次结构像下面一样;

 RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl(); 
    roleHierarchy.setHierarchy(properties.getProperty("security.roleHierarchy")); 
  • 现在你可以使用roleHierarchy.getReachableGrantedAuthoritiesAuthorityUtils得到所有可达授予的权限:

    Collection<GrantedAuthority> ga = roleHierarchy.getReachableGrantedAuthorities(AuthorityUtils.createAuthorityList(new String[]{"ROLE_ADMIN"}));