|
|
@@ -1,151 +0,0 @@
|
|
|
-package com.its.op.security;
|
|
|
-
|
|
|
-import lombok.Getter;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.security.core.Authentication;
|
|
|
-import org.springframework.security.web.DefaultRedirectStrategy;
|
|
|
-import org.springframework.security.web.RedirectStrategy;
|
|
|
-import org.springframework.security.web.WebAttributes;
|
|
|
-import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
|
-import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
|
|
-import org.springframework.security.web.savedrequest.RequestCache;
|
|
|
-import org.springframework.security.web.savedrequest.SavedRequest;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-
|
|
|
-import javax.servlet.ServletException;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import javax.servlet.http.HttpSession;
|
|
|
-import java.io.IOException;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-@Getter
|
|
|
-//@Component
|
|
|
-public class WebAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
|
|
-
|
|
|
- private final RequestCache requestCache = new HttpSessionRequestCache();
|
|
|
- private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
|
|
-
|
|
|
- private String targetUrlParameter;
|
|
|
- private String defaultUrl;
|
|
|
- private boolean useReferer;
|
|
|
-
|
|
|
- public WebAuthenticationSuccessHandler() {
|
|
|
- this.targetUrlParameter = "";
|
|
|
- this.defaultUrl = "/";
|
|
|
- this.useReferer = false;
|
|
|
- }
|
|
|
-
|
|
|
- public WebAuthenticationSuccessHandler setTargetUrlParameter(String targetUrlParameter) {
|
|
|
- this.targetUrlParameter = targetUrlParameter;
|
|
|
- return this;
|
|
|
- }
|
|
|
- public WebAuthenticationSuccessHandler setDefaultUrl(String defaultUrl) {
|
|
|
- this.defaultUrl = defaultUrl;
|
|
|
- return this;
|
|
|
- }
|
|
|
- public WebAuthenticationSuccessHandler setUseReferer(boolean useReferer) {
|
|
|
- this.useReferer = useReferer;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
|
|
- clearAuthenticationAttributes(request);
|
|
|
-
|
|
|
- int intRedirectStrategy = decideRedirectStrategy(request, response);
|
|
|
- switch (intRedirectStrategy) {
|
|
|
- case 1:
|
|
|
- useTargetUrl(request, response);
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- useSessionUrl(request, response);
|
|
|
- break;
|
|
|
- case 3:
|
|
|
- useRefererUrl(request, response);
|
|
|
- break;
|
|
|
- default:
|
|
|
- useDefaultUrl(request, response);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void clearAuthenticationAttributes(HttpServletRequest request) {
|
|
|
- HttpSession session = request.getSession(false);
|
|
|
- if (session == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
|
|
|
- }
|
|
|
-
|
|
|
- private void useTargetUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
- SavedRequest savedRequest = this.requestCache.getRequest(request, response);
|
|
|
- if (savedRequest != null) {
|
|
|
- this.requestCache.removeRequest(request, response);
|
|
|
- }
|
|
|
- String targetUrl = request.getParameter(this.targetUrlParameter);
|
|
|
- this.redirectStrategy.sendRedirect(request, response, targetUrl);
|
|
|
- }
|
|
|
-
|
|
|
- private void useSessionUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
- SavedRequest savedRequest = this.requestCache.getRequest(request, response);
|
|
|
- String targetUrl = savedRequest.getRedirectUrl();
|
|
|
- this.redirectStrategy.sendRedirect(request, response, targetUrl);
|
|
|
- }
|
|
|
-
|
|
|
- private void useRefererUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
- String targetUrl = request.getHeader("REFERER");
|
|
|
- this.redirectStrategy.sendRedirect(request, response, targetUrl);
|
|
|
- }
|
|
|
-
|
|
|
- private void useDefaultUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
- this.redirectStrategy.sendRedirect(request, response, this.defaultUrl);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 인증 성공후 어떤 URL 로 redirect 할지를 결정
|
|
|
- * 1. targetUrlParameter 값을 읽은 URL 이 존재할
|
|
|
- * 2. Spring Security 가 Session 에 저장한 URL
|
|
|
- * 3. Request 의 REFERER 를 사용하고 그 REFERER URL 이 존재할 경우
|
|
|
- * 4. Default URL
|
|
|
- *
|
|
|
- * @param request
|
|
|
- * @param response
|
|
|
- * @return 1 : targetUrlParameter
|
|
|
- * 2 : Session 에 저장되어 있는 URL
|
|
|
- * 3 : referer 헤더에 있는 url
|
|
|
- * 0 : default url
|
|
|
- */
|
|
|
- private int decideRedirectStrategy(HttpServletRequest request, HttpServletResponse response) {
|
|
|
- SavedRequest savedRequest = this.requestCache.getRequest(request, response);
|
|
|
-
|
|
|
- if (!"".equals(this.targetUrlParameter)) {
|
|
|
- String targetUrl = request.getParameter(this.targetUrlParameter);
|
|
|
- if (StringUtils.hasText(targetUrl)) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
- else {
|
|
|
- if (savedRequest != null) {
|
|
|
- return 2;
|
|
|
- }
|
|
|
- else {
|
|
|
- String refererUrl = request.getHeader("REFERER");
|
|
|
- if (useReferer && StringUtils.hasText(refererUrl)) {
|
|
|
- return 3;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- if (savedRequest != null) {
|
|
|
- return 2;
|
|
|
- }
|
|
|
-
|
|
|
- String refererUrl = request.getHeader("REFERER");
|
|
|
- if (this.useReferer && StringUtils.hasText(refererUrl)) {
|
|
|
- return 3;
|
|
|
- }
|
|
|
-
|
|
|
- return 0;
|
|
|
- }
|
|
|
-}
|