2014-11-24 71 views
-1

我使用方法級別的安全性。在類中我註釋了一些方法,表達式使用這個類的字段。但是我看到SpEL例外,我不能引用它們。 這是這個類的代碼的一部分。在表達式中,我想使用字段repPrefix,但我收到它是未知變量的異常。在Spring Security中使用類的字段進行註釋中的表達式

@Component("c2rTableManager") 
@Scope("prototype") 
public class C2RTableManager implements TableManager { 
    private final TableManager tableManager; 
    private final String repPrefix; 

    @Autowired 
    private SecurityInfoService securityInfoService; 

    public C2RTableManager(TableManager tableManager, String repository) { 
      this.tableManager = tableManager; 
      this.repPrefix = repository + "__"; 
    } 

    ...some methods 

    @Override 
    @PreAuthorize("hasRole('DBA') || hasPermission(repPrefix + #table, 'TABLE', 'DELETE_TABLE')") 
    public void dropTable(String table) throws InterruptedException, IOException { 
      tableManager.dropTable(table); 
    } 

    ...other methods 
} 

如果我寫另一種方式,表達式根本不會被評估。不明白爲什麼。

@Component("c2rTableManager") 
@Scope("prototype") 
public class C2RTableManager implements TableManager { 
    private final TableManager tableManager; 
    private final String repPrefix; 

    @Autowired 
    private SecurityInfoService securityInfoService; 

    public C2RTableManager(TableManager tableManager, String repository) { 
      this.tableManager = tableManager; 
      this.repPrefix = repository + "__"; 
    } 

    ...some methods 

    @Override 

    public void dropTable(String table) throws InterruptedException, IOException { 
      dropTable(table, repPrefix); 
    } 

    @PreAuthorize("hasRole('DBA') || hasPermission(#repPrefix + #table, 'TABLE', 'DELETE_TABLE')") 
    public void dropTable(String table, String repPrefix) throws InterruptedException, IOException { 
      tableManager.dropTable(table); 
    } 

    ...other methods 
} 

如何使用此類的字段值爲類的方法編寫表達式?

+0

是什麼確切的:SPEL例外? – Ralph 2014-11-24 17:03:45

+0

類似於未知變量,無法評估它等。 – Alex 2014-11-24 17:36:13

+0

如果使用接口AOP代理,則來自同一實例的方法調用將不會評估任何註釋。 – holmis83 2014-11-24 17:58:41

回答

0

我沒有足夠的信譽來添加評論。 從現有的春季安全文檔在http://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html

這裏我們實際使用的方法參數作爲表達 的一部分,以決定當前用戶是否具有對 給出接觸的「管理員」權限。內置的hasPermission()表達式通過應用程序上下文鏈接到Spring Security ACL模塊 中,我們將在下面看到 。您可以按名稱 表達變量訪問任何方法參數,提供您的代碼編譯調試信息 。

在最後一句,請強調。檢查以下兩點:

  • 你編譯帶有調試標誌的類嗎?
  • 你啓用這個聲明的方法級別的安全性:<global-method-security pre-post-annotations="enabled"/>
+0

我不知道aboub編譯與調試,因爲我用Maven構建應用程序並在NetBeans中進行調試。當然這個聲明是可用的,其他experssions正常工作。但爲什麼有些表達式被評估(第一個例子),但有些不是(第二個例子)? – Alex 2014-11-24 19:33:19

0

我需要聲明具有對稱性的公共

private final String repPrefix; 

和寫入鏈接註釋本

@PreAuthorize("hasRole('DBA') || hasPermission(repPrefix + #table, 'TABLE', 'DELETE_TABLE')") 
相關問題