Spring Security 3.2:不考虑@Secured注解

Posted

技术标签:

【中文标题】Spring Security 3.2:不考虑@Secured注解【英文标题】:Spring Security 3.2: @Secured annotations not taken into account 【发布时间】:2014-03-09 06:58:51 【问题描述】:

我正在尝试使用 Spring 3.2.4 和 Spring Security 3.2 使用 @Secured 注释来保护我的 RESTful API。我有以下设置:

web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:spring/*.xml
        /WEB-INF/classes/security/security-context.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Servlet configuration -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/servlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

servlet-context.xml:

<context:component-scan base-package="com.mycompany.rest.controller" />
<security:global-method-security secured-annotations="enabled" />

安全上下文.xml

<beans:bean id="merchantUserDetailsService" class="com.mycompany.rest.security.CustomUserDetailsService" /> 

<http auto-config="false" create-session="never">
    <http-basic />
</http>

<authentication-manager>
    <authentication-provider user-service-ref="customUserDetailsService" />
</authentication-manager>

我以编程方式将 customUserDetailsS​​ervice 中的自定义角色(ROLE_GROUP、ROLE_DIVISION、ROLE_READ、ROLE_WRITE)分配给用户,这工作正常。

我的一个控制器:

@Secured("ROLE_DIVISION")
@RequestMapping(method = RequestMethod.GET)
ResponseEntity<List<CustomerResource>> getCustomer() throws ResourceDoestNotExistException 
    List<Customer> providers = // retrieve providers from DAO
    List<CustomerResource> resources = customerResourceAssembler.toResources(customers);
    return new ResponseEntity<>(resources, HttpStatus.OK);

现在我的问题是,@Secured 注释被忽略了。我想使用 @Secured 注释来避免在配置中定义多个 's。当我添加至少一个时,Spring Security 工作正常,但如何避免定义它们而依赖 @Secured 注释?

我现在可以使用角色为“ROLE_GROUP”的用户访问上述方法。

【问题讨论】:

【参考方案1】:

除了启用了错误类型的注释之外,您似乎一切正常。如果您查看global-method-security 的文档,您会看到有一个名为secured-annotations 的单独属性可以启用@Secured

【讨论】:

对不起,那是我复制粘贴了错误的 sn-p 代码(正在尝试)。我更新了原帖。 它不应该影响方法拦截。如果它被应用,你应该得到一个例外。如果您确定该方法实际被调用,请检查它是否实际被代理(添加断点并查看堆栈,或添加 Thread.dumpStack() 调用)。 该方法也被 Spring Security 调用和代理。无法理解为什么这不能正常工作。接下来要尝试的是创建一个新的简单项目来进行尝试。

以上是关于Spring Security 3.2:不考虑@Secured注解的主要内容,如果未能解决你的问题,请参考以下文章

将 AccessDeniedException 传播到 Spring Security 3.2

Spring Security 3.2 CSRF 禁用特定 URL

Spring 3.2:基于 Spring Security 角色过滤 Jackson JSON 输出

Spring Security 3.2 令牌认证

Spring Security 3.2、CSRF 和多部分请求

使用 Java 配置和 Spring Security 3.2 的安全方法注释