14

看起来我的@Controller 中的方法上的@Secured 没有被读取。当使用基于 sec:intercept-url 的安全过滤时,这似乎工作得很好。以下代码导致 Spring Security 给了我这个日志条目:

调试:org.springframework.security.web.access.intercept.FilterSecurityInterceptor - 公共对象 - 未尝试身份验证

web.xml

contextConfigLocation /WEB-INF/spring/root-context.xml

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Filter 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-context.xml 包含 viewResolvers 的配置和所有编组。此配置是注释驱动的。

根上下文.xml

    <sec:global-method-security secured-annotations="enabled" />

<sec:http auto-config="true">
    <sec:http-basic/>
</sec:http>

<!-- Declare an authentication-manager to use a custom userDetailsService -->
<sec:authentication-manager>
    <sec:authentication-provider
        user-service-ref="userDetailsService">
        <sec:password-encoder ref="passwordEncoder" />
    </sec:authentication-provider>
</sec:authentication-manager>

<bean
    class="org.springframework.security.authentication.encoding.PlaintextPasswordEncoder"
    id="passwordEncoder" />
<sec:user-service id="userDetailsService">
    <sec:user name="john" password="john" authorities="ROLE_USER, ROLE_ADMIN" />
    <sec:user name="jane" password="jane" authorities="ROLE_USER" />
</sec:user-service>

PingController.java

@Controller
public class PingController {

    @Secured("ROLE_ADMIN")
    @RequestMapping(value = "/ping", method = RequestMethod.GET)
    public void ping() {
    }

}

这似乎与我使用的身份验证方法没有任何关系,因此可以忽略 basic-http-tag。

我有这样的想法,@Secured 不起作用,因为它在另一个上下文中使用,而不是在其中配置安全性的 root-context.xml。我试图将此配置移动到 servlet-context.xml,但它似乎没有到达 springSecurityFilterChain。对这个问题和我的理论有什么想法吗?

4

2 回答 2

24

你是对的,<global-method-security>是在每个上下文的基础上应用的。但是,您无需将整个安全配置移至servlet-context.xml,只需向其中添加一个<global-method-security>元素即可。

于 2011-07-11T15:27:23.913 回答
9

请参阅Spring Security FAQ(强调我的)。如果您将切入点应用到服务层,您只需要<global-method-security>在应用程序的安全上下文中进行设置。

在 Spring Web 应用程序中,为调度程序 servlet 保存 Spring MVC bean 的应用程序上下文通常与主应用程序上下文分开。它通常在名为 myapp-servlet.xml 的文件中定义,其中“myapp”是分配给 web.xml 中 Spring DispatcherServlet 的名称。一个应用程序可以有多个 DispatcherServlet,每个 DispatcherServlet 都有自己独立的应用程序上下文。这些“子”上下文中的 bean 对应用程序的其余部分不可见。“父”应用程序上下文由您在 web.xml 中定义的 ContextLoaderListener 加载,并且对所有子上下文可见。这个父上下文通常是您定义安全配置的地方,包括元素)。因此,不会强制执行应用于这些 Web bean 中的方法的任何安全约束,因为无法从 DispatcherServlet 上下文中看到 bean。您需要将声明移动到 Web 上下文或将要保护的 bean 移动到主应用程序上下文中。

通常,我们建议在服务层而不是在单个 Web 控制器上应用方法安全性。

于 2012-07-05T15:36:03.790 回答