Spring Security:使用@PreAuthorize注释不保护方法

时间:2011-02-16 21:44:07

标签: security spring role pre authorize

我想在我的托管会话bean中保护特定角色"ROLE_ADMIN"

的方法

配置(的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() {
...
    }

当我在user1anonym下登录并点击网页上的“购买”按钮时,它仍然会重定向到下一页。

我希望发生一些访问被拒绝的异常,但事实并非如此。

1 个答案:

答案 0 :(得分: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" />