Spring Security – 如何启用Method Security注释?
StackOverflow上有很多类似的问题,但我找不到任何答案:(
我有web.xml像:
contextConfigLocation /WEB-INF/spring-security.xml spring org.springframework.web.servlet.DispatcherServlet 1 contextConfigLocation /WEB-INF/spring-web.xml spring / org.springframework.web.context.ContextLoaderListener springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* FORWARD REQUEST
并尝试使用注释配置方法安全性 。 我认为它必须通过 ,放在与其他组件相同的上下文中,在我的情况下是
spring-web.xml
。 所以我有以下spring-web.xml
:
和spring-security.xml
:
ROLE_ANONYMOUS
添加<sec:global-method-security
所有控制器停止工作的问题。 我在日志中有以下内容:
PageNotFound:noHandlerFound:947 - No mapping found for HTTP request with URI [/some/page] in DispatcherServlet with name 'spring'
当我删除这个global-security
元素时,一切正常。 如果我将它添加到spring-security.xml
– 没有任何改变。 似乎没有使用它,因为任何人都可以访问使用@PreAuthorize("hasRole('ROLE_ADMIN')")
(或任何其他角色)注释的方法。
PS我正在使用Spring 3.0.5.RELEASE和Spring Security 3.0.5.RELEASE
启用
spring security会为您的控制器创建代理。 在这种情况下,spring-mvc无法在bean上找到像@RequestMapping
这样的注释。 如果要在控制器上使用安全注释,则应提取控制器的接口并在其上放置mvc注释。 Spring文档包含以下关于此的注释:
注意:使用控制器接口(例如,用于AOP代理)时,请确保在控制器接口而不是实现类上始终如一地放置所有映射注释(例如
@RequestMapping
和@SessionAttributes
。