【发布时间】:2012-06-15 12:15:26
【问题描述】:
因此,我们几乎达到了测试驱动我们的 spring web-mvc 应用程序的目标。我们正在使用 Spring Security 来保护 URL 或方法,并且我们希望使用 Spring-Web-Mvc 来测试控制器。问题是:Spring 安全性依赖于 web.xml 中定义的 FilterChain:
<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>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
虽然 spring-web-mvc 即使使用 custom context loader 也只能加载通常的应用程序上下文内容:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = WebContextLoader.class, locations = {
"file:src/main/webapp/WEB-INF/servlet-context.xml", "file:src/main/webapp/WEB-INF/dw-manager-context.xml" })
public class MvcTest {
@Inject
protected MockMvc mockMvc;
@Test
public void loginRequiredTest() throws Exception {
mockMvc.perform(get("/home")).andExpect(status().isOk()).andExpect(content().type("text/html;charset=ISO-8859-1"))
.andExpect(content().string(containsString("Please Login")));
}
}
正如我们所看到的,我们设置了我们的网络应用程序,以便在调用 /home 时会提示匿名用户登录。这与 web.xml 一起工作正常,但我们不知道如何集成它进入我们的测试。
有没有办法给spring-test-mvc添加过滤器?
现在我在想我可能必须从GenericWebContextLoader 开始,深入了解实现RequestDispatcher 的MockRequestDipatcher,以某种方式在那里添加一个过滤器,然后告诉GenericWebContextLoader 使用我的实现.. . 但是我对整个网络堆栈非常陌生,并且更喜欢一个更简单的解决方案,可以帮助我避免深入研究这些东西......
有什么想法/解决方案吗?
我将如何手动添加过滤器? web.xml 不能是唯一这样做的地方...
谢谢!
【问题讨论】:
标签: java spring spring-mvc spring-security servlet-filters