Spring security 将Spring表单从AuthenticationFailureHandler发送到控制器并创建BindingResult

Spring security 将Spring表单从AuthenticationFailureHandler发送到控制器并创建BindingResult,spring-security,Spring Security,我正在使用Spring Security开发用户身份验证 我想通过控制器方法中的@Valid注释获得Spring表单参数。首先,执行Spring安全性,然后,我需要知道如何将登录对象从AuthenticationFailureHandler发送到控制器方法 public class AuthFailureHandler implements AuthenticationFailureHandler { @Override public void onAuthentication

我正在使用Spring Security开发用户身份验证

我想通过控制器方法中的@Valid注释获得Spring表单参数。首先,执行Spring安全性,然后,我需要知道如何将登录对象从AuthenticationFailureHandler发送到控制器方法

public class AuthFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException ae) throws IOException, ServletException {

        Login login = new Login();

        login.setType(request.getParameter("type"));
        login.setEmail(request.getParameter("email"));
        login.setPassword(request.getParameter("password"));

        request.setAttribute("login", login);

        response.sendRedirect(response.encodeRedirectURL("./login/error"));
    }
}
之后,在控制器方法中,我需要创建BindingResult以在jsp Spring表单错误功能中进行仿真

现在我在AuthenticationFailureHandler和Controller方法中有了这段代码

public class AuthFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException ae) throws IOException, ServletException {

        Login login = new Login();

        login.setType(request.getParameter("type"));
        login.setEmail(request.getParameter("email"));
        login.setPassword(request.getParameter("password"));

        request.setAttribute("login", login);

        response.sendRedirect(response.encodeRedirectURL("./login/error"));
    }
}
在控制器方法中

@RequestMapping("/login/error")
public String loginError(Model model, @Valid Login login, BindingResult result) {

    String message = this.messageSource.getMessage(
            "NoAuth.loginManager",
            null,
            LocaleContextHolder.getLocale());

    ObjectError error = new ObjectError("general", message);

    result.addError(error);

    model.addAttribute("login", new Login());

    return "home";
}

您正在设置请求属性,然后执行重定向,之后该属性将丢失。您可能应该更详细地解释在身份验证失败后,您试图通过登录数据实现什么。我需要将AuthenticationFailureHandler从用户调用中获得的相同数据发送给方法控制器,从而可以毫无问题地找到@Valid和BindingResult。现在登录和BindingResult都是空的,这是我们所期望的。我的意思是,你应该编辑你的问题来解释你想要发生什么(用你的应用程序,而不是Spring控制器)。SpringSecurity不使用SpringMVC绑定,而且,作为一项规则,提交登录表单时的错误不应被视为正常的验证错误,因此我不清楚您试图实现什么。