Spring Securityでユーザー認証を開発しています。
Controller メソッドの @Valid アノテーションを介して Spring Form パラメータを取得したい。まず、Spring セキュリティが実行されます。次に、Login オブジェクトを AuthenticationFailureHandler から Controller メソッドに送信する必要があります。
その後、Controller メソッドで BindingResult を作成して、jsp Spring Form エラー機能でエミュレートする必要があります。
今、私は 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"));
}
}
In Controller メソッド
@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";
}