2012-07-13 423 views

回答

73

有一個Spring Security的特殊安全表達式:

hasAnyRole(角色列表) - 如果用戶已被授予任何 指定的角色(給出一個逗號分隔的列表的字符串)。

我從來沒有使用它,但我認爲這正是你在找什麼。

實例:

<security:authorize access="hasAnyRole('ADMIN', 'DEVELOPER')"> 
    ... 
</security:authorize> 

這裏是一個link to the reference documentation其中標準彈簧安全表達式描述。另外,這裏是一個discussion,我描述瞭如何在需要時創建自定義表達式。

+0

它的工作:-) <秒:授權訪問= 「hasAnyRole( 'ROLE_USER', 'ROLE_ADMIN')」> \t \t \t(用戶/管理員) \t可是任何方式來檢查hasAllRoles(role1,role2)? – 2012-07-13 11:40:52

+0

關於你的最後一個問題:「但是檢查hasAllRoles(role1,role2)的任何方法?」 - 我不認爲這是可能的。但你可以克里特你的自定義表達式(我給了一個鏈接),並在代碼檢查任何你想要的。 – dimas 2012-07-13 11:51:31

+4

關於你的第二個問題,你應該可以使用hasRole('ADMIN')和hasRole('DEVELOPER')這樣的'and'運算符來實現這個功能。 – bh5k 2013-04-01 21:20:28

1

JSP頁面上使用hasAnyRole可能會導致異常用於提供與單引號的角色時的方法類似下面,主要是當頁面與jQuery合併處理的JSP頁面:

<security:authorize access="hasAnyRole('ROLE_USER')"> ... 
</security:authorize> 

雖然這是贊成使用方法的棄用解決方案,我認爲解決方案提供的here也是有用的。使用替代訪問方法是ifAllGranted方法:

<security:authorize ifAllGranted="ROLE_USER"> ... 
</security:authorize> 

此外,請注意,包括JSP頁面上的以下標籤:

<%@taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> 
+4

運行客戶端的jQuery會如何影響jsp渲染,這會發生在服務器端? – 2015-07-22 17:32:34

1

我用hasAnyRole('ROLE_ADMIN','ROLE_USER'),但我是越來越以下錯誤bean創建

Error creating bean with name  'org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource] while setting bean property 'securityMetadataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Expected a single expression attribute for [/user/*] 

然後我試了

access="hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')"它的工作對我來說很好。

作爲我的用戶之一是管理員以及用戶。

爲此,你需要添加use-expressions="true" auto-config="true"其次是HTTP標籤

<http use-expressions="true" auto-config="true" >.....</http> 
4

@迪馬斯的答案是不是你的問題邏輯一致的; ifAllGranted不能直接替換爲hasAnyRole

Spring Security 3—>4 migration guide

老:

<sec:authorize ifAllGranted="ROLE_ADMIN,ROLE_USER"> 
    <p>Must have ROLE_ADMIN and ROLE_USER</p> 
</sec:authorize> 

新(SPEL):

<sec:authorize access="hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')"> 
    <p>Must have ROLE_ADMIN and ROLE_USER</p> 
</sec:authorize> 

hasAnyRole更換ifAllGranted將直接導致春季用OR而不是評估的聲明一個AND。也就是說,hasAnyRole將返回true如果認證主要包含至少一個指定的角色,而Spring的(現在已經過時,因爲Spring Security 4)ifAllGranted方法得到確認的主要包含指定角色的所有纔會返回true

TL; DR:要複製的ifAllGranted使用Spring Security的標籤庫的新的認證表達式語言行爲中,hasRole('ROLE_1') and hasRole('ROLE_2')模式需要使用。

+0

非常感謝。它同樣適用於特權。

This text is only visible to an user who has the 'CREATE_GROUP' and 'CHANGE_PASSWORD_PRIVILEGE' privileges together.
2017-09-14 08:15:15

相關問題