5

特定の役割のマネージドセッションBeanでメソッドを保護したい"ROLE_ADMIN"

config(applicationContext-security.xml):

<global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"/>
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/**" access="isAuthenticated()"/>
        <intercept-url pattern="/**" access="permitAll()"/>
        <form-login
         login-processing-url="/j_spring_security_check"
         login-page="/login.jsf"
         default-target-url="/main.jsf"
         authentication-failure-url="/login.jsf" />

    <session-management>
           <concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
    </session-management>
    </http>


    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
                <user name="user1" password="user1" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener"/>

Beanの保護された方法:

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String buy() {
...
    }

user1下または名前でログインし、anonymWebページの[購入]ボタンをクリックしても、次のページにリダイレクトされます。

アクセス拒否の例外が発生したと思いますが、発生しません。

4

1 に答える 1

5

applicationContext-security.xmlでメソッドレベルのセキュリティを有効にすることを忘れないでください。

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

PreまたはPostアノテーションを使用する場合は、次を使用します。

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

詳細については、以下を参照してください。

http://forum.springsource.org/showthread.php?t=77862

注:jsr-250からの注釈の場合:

<sec:global-method-security jsr250-annotations="enabled" />
于 2011-02-16T21:47:37.057 に答える