WebLoginSuccessHandler.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.its.op.security;
  2. import com.its.op.dao.repository.its.oper.TbUserCnncHsRepository;
  3. import com.its.op.entity.its.oper.TbUserCnncHs;
  4. import com.its.utils.CookieUtils;
  5. import com.its.utils.ItsUtils;
  6. import lombok.AllArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.security.core.Authentication;
  9. import org.springframework.security.core.GrantedAuthority;
  10. import org.springframework.security.web.WebAttributes;
  11. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  12. import org.springframework.security.web.authentication.WebAuthenticationDetails;
  13. import org.springframework.stereotype.Service;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import javax.servlet.http.HttpSession;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. @Slf4j
  22. @AllArgsConstructor
  23. @Service
  24. public class WebLoginSuccessHandler implements AuthenticationSuccessHandler {
  25. private final TbUserCnncHsRepository cnncHsRepo;
  26. @Override
  27. public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
  28. // IP, 세션 ID
  29. WebAuthenticationDetails details = (WebAuthenticationDetails)authentication.getDetails();
  30. // 인증 ID
  31. String userId = authentication.getName();
  32. log.info("login Remote-IP/Session-ID/User-ID: {}/{}/{}", details.getRemoteAddress(), details.getSessionId(), userId);
  33. // 권한 리스트
  34. List<GrantedAuthority> authList = new ArrayList<>(authentication.getAuthorities());
  35. for (GrantedAuthority auth : authList) {
  36. log.info("login Roll: {}", auth.getAuthority());
  37. }
  38. UserInfrVo userInfr = (UserInfrVo)authentication.getPrincipal();
  39. String remoteIp = ItsUtils.getHttpServletRemoteIP(request);
  40. TbUserCnncHs cnncHs = TbUserCnncHs.builder()
  41. .operSystId(userInfr.getOperSystId())
  42. .userId(userInfr.getUserId())
  43. .loginHms(ItsUtils.getSysTime())
  44. .logoutHms("")
  45. .build();
  46. this.cnncHsRepo.insertData(cnncHs.getOperSystId(), cnncHs.getLoginHms(), cnncHs.getUserId(), cnncHs.getLogoutHms());
  47. log.info("login history: {}, {}", cnncHs, remoteIp);
  48. //log.info("cnncHs: {}, {}", cnncHs.getUserId(), cnncHs.getLoginHms());
  49. String defaultSuccessUrl = "/application/op/00.main/main.html";
  50. String uri = defaultSuccessUrl;
  51. String domain = "/";
  52. HttpSession session = request.getSession();
  53. session.setAttribute(WebConstants.USER_UUID, WebMvcConfig.encUserId(cnncHs.getUserId()));
  54. session.setAttribute(WebConstants.USER_TIME, cnncHs.getLoginHms());
  55. session.setAttribute(WebConstants.LOGIN_USER, userInfr);
  56. CookieUtils.setCookie(response, WebConstants.USER_UUID, WebMvcConfig.encUserId(cnncHs.getUserId()), 60*60, domain);
  57. CookieUtils.setCookie(response, WebConstants.USER_TIME, cnncHs.getLoginHms(), 60*60, domain);
  58. CookieUtils.setCookie(response, WebConstants.USER_OPER_SYST_ID, userInfr.getOperSystId(), 60*60, domain);
  59. // Security 가 요청을 가로챈 경우 사용자가 원래 요청했던 URI 정보를 저장한 객체
  60. // RequestCache requestCache = new HttpSessionRequestCache();
  61. // SavedRequest savedRequest = requestCache.getRequest(request, response);
  62. //
  63. // // 있을 경우 URI 등 정보를 가져와서 사용
  64. // if (savedRequest != null) {
  65. // uri = savedRequest.getRedirectUrl();
  66. // // 세션에 저장된 객체를 다 사용한 뒤에는 지워줘서 메모리 누수 방지
  67. // requestCache.removeRequest(request, response);
  68. // log.info("Saved Uri: {}", uri);
  69. // }
  70. // 세션 Attribute 확인
  71. // Enumeration<String> list = request.getSession().getAttributeNames();
  72. // while (list.hasMoreElements()) {
  73. // log.info("Attribute: {}", list.nextElement());
  74. // //Attribute: TIME
  75. // //Attribute: UUID
  76. // //Attribute: SPRING_SECURITY_CONTEXT
  77. // }
  78. clearAuthenticationAttributes(request);
  79. response.sendRedirect(uri);
  80. }
  81. /**
  82. * 로그인 성공시 로그인 실패 시 작성한 에러 세션 지우기
  83. * @param request
  84. */
  85. protected void clearAuthenticationAttributes(HttpServletRequest request) {
  86. HttpSession session = request.getSession(false);
  87. //log.error("clearAuthenticationAttributes: {}", session);
  88. if (session == null) {
  89. log.warn("session already cleared.");
  90. return;
  91. }
  92. session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
  93. }
  94. }