2016-11-15 104 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」。我希望這將在下一個版本中得到解決。

相關問題