WebLoginFailureHandler.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package com.its.web.security;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.security.authentication.*;
  4. import org.springframework.security.core.AuthenticationException;
  5. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  6. import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  7. import org.springframework.stereotype.Service;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12. import java.net.URLEncoder;
  13. @Slf4j
  14. @Service
  15. public class WebLoginFailureHandler implements AuthenticationFailureHandler {
  16. @Override
  17. public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
  18. String key = "LoginFail";
  19. String message = "계정을 찾을 수 없습니다.";
  20. if (exception instanceof AuthenticationServiceException) {
  21. message ="시스템에 오류가 발생했습니다.";
  22. }
  23. else if (exception instanceof UsernameNotFoundException) {
  24. message = "아이디를 찾을 수 없습니다.";
  25. }
  26. else if (exception instanceof BadCredentialsException) {
  27. message = "아이디 또는 비밀번호가 일치하지 않습니다.";
  28. }
  29. else if (exception instanceof DisabledException) {
  30. message = "현재 사용할 수 없는 계정입니다.";
  31. }
  32. else if (exception instanceof LockedException) {
  33. message = "현재 잠긴 계정입니다.";
  34. }
  35. else if (exception instanceof AccountExpiredException) {
  36. message = "이미 만료된 계정입니다.";
  37. }
  38. else if (exception instanceof CredentialsExpiredException) {
  39. message = "비밀번호가 만료된 계정입니다.";
  40. }
  41. log.error("{}: {}, {}", key, message, request.getParameter("username"));
  42. request.setAttribute(key, message);
  43. message = URLEncoder.encode(message, "UTF-8");
  44. response.sendRedirect("/phits?LoginFail=" + message);
  45. // request.getRequestDispatcher("/phits").forward(request, response);
  46. }
  47. }