一尘不染

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

spring

它看起来不像我的@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的配置和所有编组。此配置是注释驱动的。

root-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>

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。关于这个问题和我的理论有什么想法吗?


阅读 353

收藏
2020-04-20

共1个答案

一尘不染

你是对的,<global-method-security>适用于每个per-context。但是,你无需将整个安全配置移至servlet-context.xml,只需向其中添加一个<global-method-security>元素即可。

2020-04-20