2016-11-15 70 views
2

我有从Bean_2调用方法的Bean_1。 Bean_1具有以下安全配置:Spring Security RunAsManagerImpl不起作用

<protect-pointcut expression="execution(* com.proficiency.cg.core.blc.Bean_1.*.*(..))" access="ROLE_Administrators,RUN_AS_InternalRole"/> 

Bean_2 - 有以下安全配置:

<protect-pointcut expression="execution(* com.proficiency.cg.core.blc.Bean_2.*.*(..))" access="ROLE_InternalRole"/> 

在另外的 - 我成立了RunAsManager:

<b:bean id="runAsManager" 
    class="org.springframework.security.access.intercept.RunAsManagerImpl"> 
    <b:property name="key" value="prof_key"/> 
</b:bean> 

<b:bean id="runAsAuthenticationProvider" 
    class="org.springframework.security.access.intercept.RunAsImplAuthenticationProvider"> 
    <b:property name="key" value="prof_key"/> 
</b:bean> 

<global-method-security secured-annotations="enabled" run-as-manager-ref="runAsManager" authentication-manager-ref="authenticationManager"> 

当我运行我的测试程序 - 我在访问Bean_2时遇到安全异常。 结论:RunAsManager - 无法正常工作或环礁。

回答

1

好的。看起来像RunAsManager有一个错误。虽然调试 - 我发现下面的实施原RunAsManagerImpl的:

public Authentication buildRunAs(Authentication authentication, Object object, 
     Collection<ConfigAttribute> attributes) { 
    List<GrantedAuthority> newAuthorities = new ArrayList<GrantedAuthority>(); 

    for (ConfigAttribute attribute : attributes) { 
     if (this.supports(attribute)) { 
      GrantedAuthority extraAuthority = new SimpleGrantedAuthority(
        getRolePrefix() + attribute.getAttribute()); 
      newAuthorities.add(extraAuthority); 
     } 
    } 

一切看起来都不错,但是...... 这种方法运行在所有属性(ROLE_Administrators,RUN_AS_InternalRole),并检查该字符串以“RUN_AS_”。
如果是 - (this.supports(...)) - 创建新的GrantedAuthority(getRolePrefix()+ attribute.getAttribute())。
一切都很好,但getRolePrefix()返回“ROLE_”。事实上 - 它会创建新的GrantedAuthority,如:ROLE_RUN_AS_InternalRole - 不存在!
作为一个解决方案 - 我创建了自己的RunAsManagerImpl,它覆盖了此方法,并在创建新的GrantedAuthority之前从属性中删除了“RUN_AS”。我希望这将在下一个版本中得到解决。

相关问题