認証のために authenticationManager を呼び出す前に、ログインフォームでいくつかの検証を行う必要があります。1 つの既存の投稿の助けを借りてそれを達成することができました - Spring Security ログインフォームで追加の検証を行うには?
私が正しいアプローチに従っているのか、それとも何かを見逃しているのか、誰かが私に提案してもらえますか? 特に、エラーメッセージの表示方法がよくわかりませんでした。フィルターでは、バリデーターを使用してログイン フィールドの検証を実行し、エラーが発生した場合は、(AuthenticationException を拡張する) 例外をスローし、Errors オブジェクトをカプセル化します。エラーを取得するために、 getErrors() メソッドが例外クラスに提供されます。
認証例外が発生した場合、失敗ハンドラーは例外をセッションに保存するため、私のコントローラーでは、セッションに保存されている例外をチェックし、例外が存在する場合は、バインディングの結果をエラー オブジェクトから取得したもので埋めます。私のカスタム例外(AuthenticationExceptionのランタイムインスタンスをチェックした後)
以下は私のコードスナップです -
LoginFilter クラス
public class UsernamePasswordLoginAuthenticationFilter extends
UsernamePasswordAuthenticationFilter {
@Autowired
private Validator loginValidator;
/* (non-Javadoc)
* @see org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#attemptAuthentication(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException {
Login login = new Login();
login.setUserId(request.getParameter("userId"));
login.setPassword(request.getParameter("password"));
Errors errors = new BeanPropertyBindingResult(login, "login");
loginValidator.validate(login, errors);
if(errors.hasErrors()) {
throw new LoginAuthenticationValidationException("Authentication Validation Failure", errors);
}
return super.attemptAuthentication(request, response);
}
}
コントローラ
@Controller
public class LoginController {
@RequestMapping(value="/login", method = RequestMethod.GET)
public String loginPage(@ModelAttribute("login") Login login, BindingResult result, HttpServletRequest request) {
AuthenticationException excp = (AuthenticationException)
request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
if(excp != null) {
if (excp instanceof LoginAuthenticationValidationException) {
LoginAuthenticationValidationException loginExcp = (LoginAuthenticationValidationException) excp;
result.addAllErrors(loginExcp.getErrors());
}
}
return "login";
}
@ModelAttribute
public void initializeForm(ModelMap map) {
map.put("login", new Login());
}
例外のインスタンスをチェックしてから Errors オブジェクトを取り出すコントローラーのこの部分は、クリーンなアプローチには見えません。これがそれを処理する唯一の方法なのか、それとも誰かが他の方法でアプローチしたのかはわかりません。あなたの提案を提供してください。
ありがとう!