Spring Security:登录失败后成功登录导致找不到页面

时间:2019-06-28 15:28:27

标签: java spring-mvc authentication spring-security

我已经用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";
    }
}

0 个答案:

没有答案