我想使用Spring Security JSP taglibs基於角色有條件地顯示一些內容。 但在Spring Security 3.1.x中只檢查一個角色。如何使用<sec:authorize access =「hasRole('ROLES)」>檢查多個角色?
我可以使用但是ifllllgranted已被棄用。
任何幫助?
我想使用Spring Security JSP taglibs基於角色有條件地顯示一些內容。 但在Spring Security 3.1.x中只檢查一個角色。如何使用<sec:authorize access =「hasRole('ROLES)」>檢查多個角色?
我可以使用但是ifllllgranted已被棄用。
任何幫助?
有一個Spring Security的特殊安全表達式:
hasAnyRole(角色列表) - 如果用戶已被授予任何 指定的角色(給出一個逗號分隔的列表的字符串)。
我從來沒有使用它,但我認爲這正是你在找什麼。
實例:
<security:authorize access="hasAnyRole('ADMIN', 'DEVELOPER')">
...
</security:authorize>
這裏是一個link to the reference documentation其中標準彈簧安全表達式描述。另外,這裏是一個discussion,我描述瞭如何在需要時創建自定義表達式。
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" %>
運行客戶端的jQuery會如何影響jsp渲染,這會發生在服務器端? – 2015-07-22 17:32:34
我用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>
@迪馬斯的答案是不是你的問題邏輯一致的; 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')
模式需要使用。
非常感謝。它同樣適用於特權。
它的工作:-) <秒:授權訪問= 「hasAnyRole( 'ROLE_USER', 'ROLE_ADMIN')」> \t \t \t(用戶/管理員) \t秒:授權>可是任何方式來檢查hasAllRoles(role1,role2)? – 2012-07-13 11:40:52
關於你的最後一個問題:「但是檢查hasAllRoles(role1,role2)的任何方法?」 - 我不認爲這是可能的。但你可以克里特你的自定義表達式(我給了一個鏈接),並在代碼檢查任何你想要的。 – dimas 2012-07-13 11:51:31
關於你的第二個問題,你應該可以使用hasRole('ADMIN')和hasRole('DEVELOPER')這樣的'and'運算符來實現這個功能。 – bh5k 2013-04-01 21:20:28