2012-07-13 46 views

答えて

73

春セキュリティにおける特別なセキュリティ式があります:

hasAnyRole(ロールのリスト)が - ユーザーは のいずれかの指定された役割付与されている場合は、true(カンマ区切りリストとして指定文字列の)。

私はそれを一度も使用していませんが、あなたが探しているものだと思います。

使用例:

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

ここでは、標準的なスプリングセキュリティ式が記載されているlink to the reference documentationあります。また、ここにはdiscussionがあります。必要に応じてカスタム表現を作成する方法について説明しました。それはあるけど

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

:ほとんどのページがjQueryを使って合成され、以下のようにメソッドに単一引用符での役割を提供する際に、JSPページを処理するために例外を引き起こす可能性がJSPページにhasAnyRoleを使用して

+0

hasAllRoles(role1、role2)? –

+0

あなたの最後の質問について:「しかし、hasAllRoles(role1、role2)をチェックする方法はありますか?」 - それは可能だとは思わない。しかし、あなたはあなたのカスタム表現を(私はリンクを与えて)クレジットすることができ、コードではあなたが望むものをチェックします。 – dimas

+4

2番目の質問では、hasRole( 'ADMIN')やhasRole( 'DEVELOPER')のような 'and'演算子で 'hasRole'を使用してこの機能を実現できるはずです。 – bh5k

1

アクセスメソッドに賛成の廃止されたソリューション、私は解決策を提供すると考えてhereも便利です。また

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

、JSPページに次のタグを含めるように注意してください:代わりのアクセス方法のifAllGrantedメソッドを使用して

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

jQueryはクライアントサイドで動作し、jspレンダリングにどのような影響を与えますか? –

1

私はhasAnyRole('ROLE_ADMIN','ROLE_USER')を使用しますが、私はエラー

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')"とその仕事私のために大丈夫です。

私のユーザーの1人はadminでもuserでもあるので。このため

あなたがディマスの答えはあなたの質問と、論理的に矛盾していない

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

@のhttpタグが続くuse-expressions="true" auto-config="true"を追加する必要があります。 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。これは、認証されたプリンシパルが指定された役割の少なくとも1が含まれている場合、認証されたプリンシパルが指定された役割のすべてが含まれている場合にのみtrueを返さhasAnyRoleは、SpringのifAllGranted方法(今春のセキュリティ4の非推奨)に対し、trueを返します、です。

TL; DR:春のセキュリティTaglibの新認証式言語を使用してifAllGrantedの動作を再現するには、hasRole('ROLE_1') and hasRole('ROLE_2')パターンを使用する必要があります。 \t \t \t(ユーザー/管理者) \tしかし、かどうかを確認する方法:それは<アクセスを許可= "hasAnyRole( 'ROLE_USER'、 'ROLE_ADMIN')" 秒> :-)働い

+0

ありがとうございました。同様に特権のために働く。

This text is only visible to an user who has the 'CREATE_GROUP' and 'CHANGE_PASSWORD_PRIVILEGE' privileges together.

関連する問題