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

我想在我的托管会话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() {
...
    }

当我以user1或匿名登录并单击网页上的“购买”按钮时,它仍然会重定向到下一页.

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

解决方法:

请记住在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" />
上一篇:企业微信获取code,URL详解


下一篇:检查身份证号