• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

6분20초쯤에

22.02.07 22:20 작성 조회수 184

0

부모클래스의 onAuthenticationFailure를 호출한 이유가 이해가 가질 않는데..왜그런거죠..?

답변 1

답변을 작성해보세요.

1

인증에 실패하게 되면 후속처리를 하게 되는데 강의에서 보면

 부모 클래스인 SimpleUrlAuthenticationFailureHandler 의 setDefaultFailureUrl("/login?error=true&exception.. ) 를 호출하고 있습니다.

public void setDefaultFailureUrl(String defaultFailureUrl) {
Assert.isTrue(UrlUtils.isValidRedirectUrl(defaultFailureUrl),
() -> "'" + defaultFailureUrl + "' is not a valid redirect URL");
this.defaultFailureUrl = defaultFailureUrl;
}

즉 예외메시지를 담아서 로그인 페이지로 이동하도록 설정하기 위한 구문입니다.

그리고 defaultFailureUrl 에 저장된 이동페이지 정보를 가지고 포워딩 혹은 리다이렉트 하는 처리를 합니다.

실은 이 처리를 직접 코드를 작성해서 구현할 수 있지만 부모 클래스인 SimpleUrlAuthenticationFailureHandler 에서 하고 있기 때문에 super.onAuthenticationFailure(request, response, exception) 를 호출하고 있습니다.

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
if (this.defaultFailureUrl == null) {
if (this.logger.isTraceEnabled()) {
this.logger.trace("Sending 401 Unauthorized error since no failure URL is set");
}
else {
this.logger.debug("Sending 401 Unauthorized error");
}
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
return;
}
saveException(request, exception);
if (this.forwardToDestination) {
this.logger.debug("Forwarding to " + this.defaultFailureUrl);
request.getRequestDispatcher(this.defaultFailureUrl).forward(request, response);
}
else {
this.redirectStrategy.sendRedirect(request, response, this.defaultFailureUrl);
}
}

onAuthenticationFailure 메소드 내에서 defaultFailureUrl 에 저장된 정보를 가지고 후속 처리를 하고 있습니다.

H K님의 프로필

H K

2023.08.07

필요한 정보만 CustomAuthenticationFailureHandler 에서 실행하고 나머지 실행은 원래 기본적으로 제공되는

SimpleUrlAuthenticationFailureHandler 코드를 사용했다고 생각하면 되나요 ?