【发布时间】:2013-01-11 17:51:46
【问题描述】:
我正在尝试使用 mvc-test 测试我的登录页面。 在添加 spring 安全性之前,我的工作非常好。
我的代码是:
mockMvc.perform(
post("j_spring_security_check")
.param(LOGIN_FORM_USERNAME_FIELD, testUsernameValue)
.param(LOGIN_FORM_PASSWORD_FIELD, testPasswordValue))
.andDo(print())
.andExpect(status().isOk())
.andExpect(model().attribute(LOGIN_PAGE_STATUS_VALUE, LOGIN_PAGE_STATUS_FALSE_INDICATOR));
测试类添加了正确的注释:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:security-context.xml", "classpath:applicationContext.xml", "classpath:test-contexts/test-context.xml" })
我的过滤器已定义(在 web.xml 中):
<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>
当我尝试在 @ContextConfiguration 中添加 web.xml 时,它失败了,当我删除它时,我得到一个异常:
java.lang.AssertionError: Status expected:<200> but was:<405>
有什么方法可以添加 DelegatingProxyFilter 来测试上下文,并在我的 security-context.xml 中定义配置以使其正常工作?我尝试了一些关于注入 FilterProxyChain 的教程,但在我的情况下它不起作用。
有人可以帮我吗? 提前致谢
【问题讨论】:
-
不要认为你可以通过这种方式测试安全过滤器,因为它不是托管的完整的 http 服务器,它只是解决 spring-mvc-test 提供的 rls 的一种方法。您可能只需要对身份验证位中的各个部分进行单元测试。
标签: spring-mvc spring-security spring-test-mvc