5

我想在我的托管会话 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或身份登录anonym并单击网页上的“购买”按钮时,它仍然重定向到下一页。

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

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 回答