preAuthorize注释似乎不起作用

时间:2012-04-03 13:37:54

标签: spring-security spring-aop

我们正在尝试使用Spring的@preAuthorize标记来提高方法级安全性。一切都编译好并且运行良好,但限制不会发生。这是一种仅限管理员的方法,但即使是非管理员也可以访问。以下是我们在上下文中配置它的方法:

<http use-expressions="true" create-session="never"  entry-point-ref="oauthAuthenticationEntryPoint" xmlns="http://www.springframework.org/schema/security" authentication-manager-ref="authManager">
    <intercept-url pattern="/mocks/some-service" access="hasRole('ROLE_OTHER')" />
    <form-login authentication-failure-url="/login.jsp" default-target-url="/login.jsp" login-page="/login.jsp"></form-login>
    <custom-filter ref="resourceServerFilter" after="EXCEPTION_TRANSLATION_FILTER" />

  </http>

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

以下是方法本身:

@PreAuthorize("(hasRole('ROLE_ADMIN'))")
public Some doIt(Some Input) {
            do something;
    return some;
}

但每个人都可以拥有一些东西。我错过了什么?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:15)

事实证明,您无法对从同一个类中访问的方法进行注释,无论是私有还是公共。注释仅适用于外人访问的公共方法。

希望能帮助某人犯同样的错误。

答案 1 :(得分:6)

请看一下:http://static.springsource.org/spring-security/site/faq/faq.html#faq-method-security-in-web-context

总结一下:

  

“您需要将声明移至   Web上下文或将您想要保护的bean移动到main中   应用程序上下文。“

相关问题