登录 jsp 表单和 spring security xml 配置如下:
<spring:url value="/j_spring_security_check" var="login" />
<form action="${login}" method="POST">
<fieldset>
<legend>Login</legend>
<input type="text" name="j_username" id="username" placeholder="Usename"/>
<input type="text" name="j_password" id="password" placeholder="Password"/>
<input type="submit" value="Login" />
</fieldset>
</form>
...
<security:form-login login-page="/public/authentication/login.htm"
login-processing-url="/j_spring_security_check"
default-target-url="/public/index.htm"
authentication-failure-url="/public/authentication/login.htm?authenticationNok=1"/>
这是表单汇总的测试:
@Test
public void testLoginPostController() throws Exception {
Account account = new AccountBuilder("test", "test", "test@gmail.com", Address.FAKE_EMPTY_ADDRESS4TESTS)
.build();
this.mockMvc.perform(post("/j_spring_security_check").param("j_username", account.getUsername()).param("j_password", "test"))
.andDo(print())
.andExpect(status().isMovedTemporarily())
.andExpect(view().name("redirect:/public/index.htm"));
}
但我得到:java.lang.AssertionError: Status expected:<302> but was:<404>
当我在浏览器中打开登录页面时,我看到生成的表单是:
<form action="/SpringMvcExample/j_spring_security_check" method="POST">
好的,我尝试将测试更改为:
this.mockMvc.perform(post("/SpringMvcExample/j_spring_security_check").param("j_username", account.getUsername()).param("j_password", "test"))
但是得到了同样的结果。同时,当我在浏览器中提交登录表单时,它会将我重定向到public/index.htm
页面,正如测试中所期望的那样。
我究竟做错了什么?