2013-01-10 36 views
3

以Spring Security 3.1.3.RELEASE春季安全@PreAuthorize - 通過使用Spring EL限制某些角色

因此,如果有角色(10)的名單,有必要訪問封鎖只有一個一個Spring控制器方法。這可以使用Spring表達式語言來完成,並避免列出每個和非常被接受的角色?

例如,通過包含Not符號。

@PreAuthorize( 「!hasRole( 'ROLE_FREE_USER')」)

在列出的所有角色這樣

@PreAuthorize(「hasAnyRole( 'ROLE_ADMIN','ROLE_PAID_USER 」, 'ROLE_PREM_USER',...)

我看過的文檔在這裏:http://static.springsource.org/spring-security/site/docs/3.0.x/reference/el-access.html

但似乎沒有任何案件不平等。任何人都有類似的問題

回答

2

我非常肯定,在Spring表達式語言(SPEL)中不支持NOT-SIGN(!)。當然,它返回boolean的結果。

爲例從official documentation

// evaluates to false 
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class); 

// -- AND and NOT -- 
String expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')"; 
boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class); 
+1

感謝您的文檔。你是對的SPEL而不是簽名。以下是最新的文檔:http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/expressions.html#expressions-language-ref – MasterV