221216_데이트 장소 다이어리 제작 10_자동 로그인 기능 수정

창고·2022년 12월 19일
0

해당 게시글은 개인 프로젝트인 "데이트 장소 다이어리 제작"
#34 "로그아웃 기능 수정 및 자동 로그인 기능 재활성화" 이슈를 다루고 있습니다.

1. 진행 사항

  • 자동 로그인 관련해서 로그아웃 시 SQLExecption이 발생하는 원인을 찾았다... persistent_logins 테이블을 DDL로 만들어주지 않았기 때문; 테이블이 없었기 때문에 token이 저장되지 않았고 당연히 로그아웃 시 없는 테이블에서 token을 지우려고 했기 때문에 오류가 발생하였음
create table persistent_logins
(
    username  varchar(64) not null,
    series    varchar(64) primary key,
    token     varchar(64) not null,
    last_used timestamp   not null
)
  • AuthenticationFailureHandler를 구현하여 로그인 실패 시 화면 상에서 메시지가 출력되도록 설정
public class CustomLoginFailureHandler implements AuthenticationFailureHandler {

    private final String DEFAULT_FAILURE_URL = "/user/login?error=true";

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

        String errorMessage = null;

        if (exception instanceof UsernameNotFoundException) {
            errorMessage = "계정이 존재하지 않습니다.";
        }
        else if (exception instanceof BadCredentialsException || exception instanceof InternalAuthenticationServiceException) {
            errorMessage = "아이디나 비밀번호가 맞지 않습니다.";
        }
        else {
            errorMessage = "알 수 없는 이유로 로그인에 실패하였습니다.";
        }

        request.setAttribute("errorMessage", errorMessage);
        request.getRequestDispatcher(DEFAULT_FAILURE_URL).forward(request, response);
    }
}

2. 결과

  • 자동 로그인 정상 작동되는 점 확인
  • 로그인 실패 시 오류 메시지 출력

3. 개선이 필요한 점

  • javascript로 인증 실패를 확인하여 alert을 띄우는 방법을 찾아봐야겠다
profile
공부했던 내용들을 모아둔 창고입니다.

0개의 댓글