2016년 12월 25일 일요일

Spring Security Login Error 메시지 처리

Spring Security 인증 방법에 대한 글을 쓴 적이 있다.

그런데 Login Fail의 경우 Error 메세지 처리를 해야할 것이 아닌가?

provider나 userDetailService의 경우
각 케이스 별로 UsernameException, BadCredentialException등의 예외를 던지면

Exception error = (Exception) session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");if (error != null) {
    model.addAttribute("error", error.getMessage());
}

session에서 `SPRING_SECURITY_LAST_EXCEPTION`이라는 키로 해당 예외에서 생성한 exception object를 가져올 수 있다.

처리방법은 여러가지가 있겠지만, 단순히 메세지만 처리하기 위함이라면
Exception으로 캐스팅한 후에 getMessage() 메서드로 해당 메시지를 가져 올 수 있다.


댓글 없음:

댓글 쓰기