【发布时间】:2015-01-01 22:34:34
【问题描述】:
我在使用 MockMvc 测试 Spring Boot 应用程序时遇到了一些问题。
我有以下测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {SpringConfiguration.class, SecurityConfiguration.class})
@IntegrationTest({"server.port=8080"})
@WebAppConfiguration
public class DemoTest {
@Autowired
private EmbeddedWebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testGetAccountUnauthenticated() throws Exception {
mockMvc.perform(get("/accounts/1").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized());
}
}
这会导致 HTTP 200 而不是 401。我启用了组件扫描和自动配置,并且在我的 SecuityConfiguration 类中配置了 spring 安全性,如下所示:
@Configuration
@EnableWebSecurity
@EnableWebMvcSecurity // required for use of @AuthenticationPrincipal in MVC controllers.
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
web.debug(true);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//set up authentication.
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated();
// set up form login
}
}
如果我使用 RestTemplate 访问 http://localhost:8080/accounts/1,那么我会得到预期的行为 (HTTP 401)。
我看到其他示例(例如Spring Boot setup security for testing)建议我自动连接FilterChainProxy 并使用WebApplicationContext.addFilters(filterChainProxy) 方法手动添加过滤器。但是,这对我来说实际上是失败的 (org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.web.FilterChainProxy] found)。
我有两个问题:
- 为什么注入的 WebApplicationContext 不会自动使用 SpringSecurity 过滤器?即使我可以获取 FilterChainProxy 并手动添加,JavaDoc for EmbeddedWebApplicationContext 状态
上下文中定义的任何 {@link Servlet} 或 {@link Filter} bean 都将自动注册到嵌入式 Servlet 容器中
因此,我不希望手动添加安全过滤器链,因为我(错误地?)由于 Spring Boot 中的自动配置魔法,我希望它“正常工作”?
- 为什么应用上下文中没有FilterChainProxy?同样,也许我对 AutoConfiguration 的期望不正确 - 但我认为这将被配置为上下文配置的一部分。
提前感谢您的任何建议。
编辑
-
FilterChainProxy 没有被注入的原因是我的配置设置为
公共无效配置(WebSecurity web){ web.debug(true); }
这实际上配置了org.springframework.security.web.debug.DebugFilter。无论此调试设置如何,我现在设法获取过滤器的方式如下:
@Resource(name = AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME)
private Filter securityFilter;
如果我将其添加到 MockMvcBuilder 中,如下所示:
MockMvcBuilders.webAppContextSetup(webApplicationContext).addFilters(securityFilter)
然后它会按预期工作。
但是,我不明白为什么 MockMVC 会忽略过滤器,因为这对于测试请求似乎很重要,因为过滤器中可能发生任何可能影响测试结果的事情。此外,这意味着要正确测试,我需要在 servlet 上下文中查找所有过滤器并建立它们的优先级/url 映射并适当地添加它们。这似乎容易出错且没有必要。
【问题讨论】:
-
好的,所以我还没有解决问题。但是,我相信 MockMVC 只是忽略了 EmbeddedWebApplicationContext 上的任何配置过滤器。在我的调试器中,如果我评估表达式
webApplicationContext.getServletContext().getFilterRegistrations(),我可以看到一堆过滤器,包括“springSecurityFilterChain”。虽然这并没有向我解释为什么不能注入 FilterChainProxy,但它也提出了一个问题,即为什么 MockMvc 会简单地忽略在 Servlet 上下文中配置的过滤器。 -
正确。这就是它被称为 MockMVC 的原因(它主要用于测试“DispatcherServlet”和 Spring MVC)。但是您的测试中有一个完整的应用程序在端口 8080 上运行,那么如果您想要端到端测试,为什么不直接使用
RestTemplate来实现呢? -
我选择使用 MockMVC 而不是 RestTemplate 的主要原因是我觉得 API 更易于使用,从而产生更简洁的测试代码并符合预期 - 例如“.andExpect(status().isUnauthorized())”。其次,我觉得测试以不同用户的身份发出请求会稍微容易一些,而不必担心传递特殊的 cookie 值或请求标头以确保我作为给定用户进行了身份验证。无论哪种方式,我都决定完全按照您的建议使用 RestTemplate 而不必担心 MockMVC。
标签: spring-security spring-boot spring-test spring-test-mvc mockmvc