WebLoginFailureHandler.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.its.op.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.RequestDispatcher;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.IOException;
  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. RequestDispatcher dispatcher = request.getRequestDispatcher("/login.html");
  44. dispatcher.forward(request, response);
  45. // request.setAttribute("username", request.getParameter("username"));
  46. // request.getRequestDispatcher("/login_view?error=true").forward(request, response);
  47. }
  48. }