• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

failurehandler 경로 질문

21.03.24 10:13 작성 조회수 542

0

안녕하세요 선생님, 질문이 한가지 있습니다.

다름아니라 failurehandler에서 setDefaultFailureUrl을 

/login~ 형식이 아니라 

/way 형식으로 하려 하는데

그러면 그 경로로 가면 입력한 유저의 정보를 어떻게 가져올 수 있을지 궁금합니다.

제가 생각했을 때는 SimpleUrlAuthenticationFailureHandler 를 사용하기 보다 

ExceptionMappingAuthenticationFailureHandler 를 사용해서 해당 url 로 request와 response 를 매핑해줄 수 있는게 아닌가 싶습니다.

ExceptionMappingAuthenticationFailureHandler 를 받고 난 후에 

아래와 같이 불러오면 되는건지 선생님의 의견이 필요합니다

@GetMapping(value = "/way")
public String twoFactor(HttpServletRequest request, HttpServletResponse response) throws Exception{

답변 4

·

답변을 작성해보세요.

1

Failurehandler 에서 입력한 유저의 정보를 Controller에 전달해서 가지고 오는 방법은 redirectUrl 에 직접 유저 정보의 파라미터를 추가해서 전달해야 합니다.

그리고 기본적으로 redirectUrl 은 포워드 방식이 아니고 sendRedirect 방식이기 때문에 request.setAttribute(key, value) 로 저장한 값을 가지고 올 수 없습니다.

SimpleUrlAuthenticationFailureHandler 에 보시면 forwardToDestination 속성이 있는데 default 는 false 입니다.

이 값을 true 로 설정하면 포워딩 방식으로 변경되어 request 에 저장한 값들을 Controller 에서 받아 올 수 있습니다.

public void setUseForward(boolean forwardToDestination) {
this.forwardToDestination = forwardToDestination;
}
public void onAuthenticationFailure(HttpServletRequest request,
HttpServletResponse response, AuthenticationException exception)
throws IOException, ServletException {

if (defaultFailureUrl == null) {
logger.debug("No failure URL set, sending 401 Unauthorized error");

response.sendError(HttpStatus.UNAUTHORIZED.value(),
HttpStatus.UNAUTHORIZED.getReasonPhrase());
}
else {
saveException(request, exception);

// 이 부분이 포워딩 방식으로 다시 요청합니다.
if
(forwardToDestination) {
logger.debug("Forwarding to " + defaultFailureUrl);

request.getRequestDispatcher(defaultFailureUrl)
.forward(request, response);
}
else {
logger.debug("Redirecting to " + defaultFailureUrl);
redirectStrategy.sendRedirect(request, response, defaultFailureUrl);
}
}
}

저도 사실 저렇게는 사용해 보지 않아서 후속처리가 어떻게 될 지 정확하게는 모르겠으나 url 에 파라미터를 직접 추가하지 않고 데이터를 Controller 에 전달하는 방법 중 request 에 값을 저장해서 가져 올 수 있지 않나 싶습니다.

한 번 시도해 보십시오

0

김유진님의 프로필

김유진

질문자

2021.03.25

선생님의 하예와 같은 친절함에 감탄합니다..

ㅠㅠㅠ 너무 감사드립니다..!!

정말 선생님 대단하신거 같습니다..!!

0

김유진님의 프로필

김유진

질문자

2021.03.24

아래는 ExceptionMappingAuthenticationFailurehandler를 상속받은  failurehandler 클래스 입니다 

아래는 UsernamePasswordAuthenticationFilter를 상속받는 클래스의 내용입니다

0

혹시 소스 공유 가능할까요?