我已经用Spring Security配置了网站的身份验证。问题在于,登录失败后,Spring会将?error
附加到URL(这很好,因为我使用它来显示错误)。但是,当用户从第二次尝试成功登录时,他们看到找不到错误404。但是,如果用户从第一次尝试成功登录,则不会发生这种情况。我该如何解决这个问题?
登录页面login.jsp
如下所示:
<c:choose><c:when test="${param.error != null}">
<div class="error"">Invalid user or password</div>
</c:when></c:choose>
<form action="<c:url value="/login"/>" method="POST" novalidate="" accept-charset="UTF-8">
<label for="username">Login</label>
<input type="text" name="username" id="username" required />
<label for="password">Password</label>
<input type="password" name="password" id="password" required />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<input type="submit" name="login" value="Login" id="login-submit" />
</form>
通过以下方式配置SecurityConfig:
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated();
http.formLogin().loginPage("/login").permitAll().and().logout().logoutSuccessUrl("/");
http.logout().logoutUrl("/logout");
}
下面是我的控制器类:
@Controller
@RequestMapping("/")
public class MainController {
@RequestMapping(value = "login")
public String login() {
return "login";
}
}