@Secured在控制器中不起作用,但intercept-url似乎工作正常

kar*_*lak 14 spring spring-mvc spring-security spring-annotations

看起来我的@Controller中的方法看起来不像@Secured.当使用基于sec:intercept-url的安全过滤时,这似乎工作正常.以下代码导致Spring Security向我提供此日志条目:

DEBUG: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>
Run Code Online (Sandbox Code Playgroud)

servlet-context.xml包含viewResolvers和所有编组的配置.此配置是注释驱动的.

根的context.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>
Run Code Online (Sandbox Code Playgroud)

PingController.java

@Controller
public class PingController {

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

}
Run Code Online (Sandbox Code Playgroud)

这似乎与我正在使用的身份验证方法无关,因此基本的http-tag可能会被忽略.

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

axt*_*avt 24

你是对的,<global-method-security>是按照每个上下文应用的.但是,您不需要将整个安全配置移动到servlet-context.xml,只需向其添加<global-method-security>元素即可.


Tom*_*asz 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控制器上应用方法安全性.