2012-08-07 16 views
21

私は多くの同様の質問を見つけましたが、誰も私の問題を解決しませんでした。 私の問題はROLE_USERの機能にアクセスできますROLE_ADMIN@PreAuthorize注釈が動作しないスプリングセキュリティ

私のspring-security.xmlコードは以下のとおりです。私は<s:global-method-security pre-post-annotations="enabled"/> 私のコードを追加し

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:s="http://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

<s:http auto-config="true" use-expressions="true"> 
    <s:intercept-url pattern="/index.jsp" access="permitAll" /> 
    <s:intercept-url pattern="/welcome*" access="hasRole('ROLE_USER')" /> 
    <s:intercept-url pattern="/helloadmin*" access="hasRole('ROLE_ADMIN')" /> 

    <s:form-login login-page="/login" default-target-url="/welcome" 
     authentication-failure-url="/loginfailed" /> 
    <s:logout logout-success-url="/logout" /> 
</s:http> 

<s:authentication-manager> 
    <s:authentication-provider> 
    <s:user-service> 
     <s:user name="asif" password="123456" authorities="ROLE_USER,ROLE_ADMIN" /> 
     <s:user name="raheel" password="123456" authorities="ROLE_USER" />   
    </s:user-service> 
    </s:authentication-provider> 
</s:authentication-manager> 

は、リソースが見つからないというエラーが表示され、私は自分のコードを削除すると正常に実行され、 ROLE_USERROLE_ADMIN機能

私のコントローラ機能があるにアクセスすることができます。

@PreAuthorize("hasRole('ROLE_ADMIN')") 
@RequestMapping(value="/delete", method = RequestMethod.GET) 
public String DeleteAll(ModelMap model, Principal principal) { 

    org.springframework.security.core.userdetails.User activeUser = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
    System.out.println("Active user is "+activeUser.getUsername()+"Authorities are "+activeUser.getAuthorities()); 
    return "deleteUsers"; 

} 

答えて

21

あなたは@PreAuthorize注釈が仕事をしたい場合は、

<s:global-method-security pre-post-annotations="enabled"/> 

を持つ必要があります。あなたがspring-aop依存関係が欠落しているように見えます


はコメントに答えるために。

あなたが必要なMavenの使用している場合:あなたはhereからjarファイルを取得することができない場合は

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-aop</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

を。

+0

はい私が追加したとき、私は知っているが、 私のコードは、リソースが見つからないというエラーを示して、あなたはそれを修正する方法を知っていますか? – Raheel

+0

org.springframework.web.context.ContextLoaderListenerクラスのリスナーインスタンスにコンテキスト初期化イベントを送信する例外];ネストされた例外はjava.lang.NoClassDefFoundErrorです:org/aopalliance/intercept/MethodInterceptor – Raheel

+0

と私はspring security3.07を使用しています – Raheel

3

次にあなたが

@Secured("ROLE_ADMIN") 
@RequestMapping(value="/delete", method = RequestMethod.GET) 
public String DeleteAll(ModelMap model, Principal principal) { 

    ... 

} 

はここa detailed blog post about itだでしょう、@Securedアノテーションで

を試してみてください。

1

これはあなたを助けるかもしれない:

<security:global-method-security secured-annotations="enabled" proxy-target-class="true"/> 

よろしく。

+0

大きなおかげで!それは私の問題を解決しました。 – WhiteWater

2

上記の回答のどれも私のために働いていません。私はセキュリティデコレータを追加するルートに行かなければならなかった。

デコレータは、servlet-context.xmlファイル内のBeanに配置されます。

まずXML名前空間にセキュリティスキーマを追加します。

<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
... 
xmlns:security="http://www.springframework.org/schema/security" 
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

をサーブレットを使用しているとき..だから

<beans:bean id="myService" class="my.service.impl.MyServiceImpl" scope="request"> 
    <security:intercept-methods> 
     <security:protect access="ROLE_USER" method="get*"/> 
     <security:protect access="ROLE_ADMIN" method="set*"/> 
    </security:intercept-methods> 
</beans:bean> 
1

この問題が発生しますように続いて、サービスBeanの実装にデコレータを適用します3とWeb非同期サポート。 Spring Security 3.1.4以下は、一旦、Callableメソッドの匿名メソッドを入力すると、セキュリティコンテキストを失います。

Spring Security to 3.2.0 RC1をアップグレードすると、問題が解決します。

Mavenの依存関係:

<dependencies> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>3.2.0.RC1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>3.2.0.RC1</version> 
    </dependency> 
</dependencies> 

<repositories> 
    <repository> 
     <id>spring-milestones</id> 
     <name>Spring Milestones</name> 
     <url>http://repo.spring.io/milestone</url> 
     <snapshots> 
      <enabled>false</enabled> 
     </snapshots> 
    </repository> 
</repositories> 
<repositories> 
    <repository> 
     <id>spring-milestones</id> 
     <name>Spring Milestones</name> 
     <url>http://repo.spring.io/milestone</url> 
     <snapshots> 
      <enabled>false</enabled> 
     </snapshots> 
    </repository> 
</repositories> 
13

私は同じ問題に直面していました。下の要素をapplicationContext.xmlから* -servlet.xml(私のディスパッチャの設定xml)に移動すると、私の問題は解決しました。

<security:global-method-security secured-annotations="enabled"/> 

は、あなたのアプリケーションのXMLではなく、あなたのディスパッチャのXMLにこの要素を含める必要があります。
Spring FAQ

+1

サーブレットのXML設定で定義されているglobal-security-methodを少なくとも持っていない場合、これは間違いなく正しい回答です。 – chrismarx

+0

これは厳密な問題です。 –

21

次の属性を追加することを忘れないでくださいXMLコンフィギュレーションを使用している場合:あなたは、以下の注釈を追加することを忘れないでくださいJavaの設定を使用している場合

<s:global-method-security pre-post-annotations="enabled"/>

を:

@EnableGlobalMethodSecurity(prePostEnabled = true)

0

タグを別のスプリングコンフィグレーションファイルに入れてください。スプリングコンフィグレーションファイルではありません。 e。

同じエラーが発生しました。

<security:global-method-security secured-annotations="enabled" proxy-target-class="true"/> 
関連する問題