看起来我的@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。对这个问题和我的理论有什么想法吗?