2012-03-21 117 views
0

我是Spring Security的新手。我一直致力於創建一個自定義選舉器,根據對象的屬性值決定是否授予權限。也就是說,如果對象實例A具有值爲i的屬性X,則具有ROLE_MGR的用戶具有訪問權限。如果對象實例B在X屬性中具有值j,則ROLE_MGR不具有訪問權限。是否有可能這樣做,如果是這樣,我需要做什麼?如果這是不可能的,我們可能決定不使用Spring Security。是否可以使用對象的屬性值來決定訪問權限?

+0

的確是這樣,但你到底是在實施它有什麼樣的問題?這看起來非常簡單。 – Simeon 2012-03-21 12:28:40

+0

我正在使用自定義選舉器(實現AccessDecisionVoter),但是沒有辦法獲取域對象。我如何檢查自定義選舉器中域對象的屬性值,還是需要查看另一個接口/類來自定義? – chanakya2 2012-03-21 16:45:40

+0

我認爲你需要更多地解釋「對象」。它在哪裏以及如何確定要訪問哪個實例?這似乎基本上就是答案所在。如果它無法從投票人那裏獲得(例如通過注入一個DAO),那麼你需要解釋爲什麼。 – 2012-03-21 17:32:41

回答

0

我想通了。我需要使用自定義權限評估程序。從我的代碼摘錄以下提供的人可能會試圖做同樣的事情:

的security.xml

<security:global-method-security 
    pre-post-annotations="enabled"> 
    <security:expression-handler ref="expressionHandler" /> 
</security:global-method-security> 

<bean id="expressionHandler" 
    class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler"> 
    <property name="permissionEvaluator"> 
     <bean id="permissionEvaluator" 
      class="org.krams.tutorial.infrastructure.SomePermissionsEvaluator" /> 
    </property> 
</bean> 

服務接口 @PostFilter( 「調用hasPermission(filterObject, '讀')」) public List getAll();

自定義權限計算器

@Override 
public boolean hasPermission(Authentication authorities, 
     Object targetDomainObject, Object permission) { 

    boolean Decision = false; 
    System.out.println("Initial Decision: " + Decision); 

    Date cutoffDate = null; 
    try { 
     cutoffDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH) 
       .parse("January 1, 2012"); 
     System.out.println("Cutoff Date: " + cutoffDate.toString()); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    System.out.println("Domain Object Date: " 
      + Post.class.cast(targetDomainObject).getDate()); 

    if (Post.class.cast(targetDomainObject).getDate().before(cutoffDate)) { 
     Decision = false; 
     System.out.println("In before"); 
    } else { 
     Decision = true; 
     System.out.println("In after"); 
    } 
    System.out.println("Final Decision: " + Decision); 
    System.out.println("--------"); 
    return Decision; 
} 
0

這可能,但首先看看Spring Security的域對象安全。這是用來授予細粒度訪問你的對象,看到這裏:http://static.springsource.org/spring-security/site/docs/3.0.x/reference/domain-acls.html

+1

我已經閱讀了acl的文檔,並沒有指定我如何根據域對象的屬性的值作出決定。 – chanakya2 2012-03-21 16:42:15

+0

我同意,OP希望基於域對象屬性的值而不是域對象本身進行保護。 – 2016-05-27 04:31:48

相關問題