검색을 해 보면 대부분 REFERER 를 이용하여 처리하게 했는데 뭔가 적절하지 않은 솔류션이 아닌가 싶다. 대개 처음 페이지가 띄어져 있는 상태에서 임의의 메뉴를 선택할 때 로그인이 되어 있지 않으면 로그인 페이지로 이동하게 된다. 로그인이 성공하면 그 임의의 메뉴로 이동을 해야 하는데.. REFERER를 이용하여 처음 페이지로 이동하게 된다.
스프링 시큐리티를 사용하고 있다고 하면 spring-security.xml 에 아래처럼 처리한다.
<sec:form-login
login-page="/login/login"
username-parameter="userid" password-parameter="password"
authentication-failure-url="/login/login?error=true"
authentication-success-handler-ref="customAuthenticationSuccessHandler" />
org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler 를 참고하여 CustomAuthenticationSuccessHandler 을 구현하면 된다.
RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
RequestCache requestCache = new HttpSessionRequestCache();SavedRequest savedRequest = requestCache.getRequest(request, response);
String targetUrl = savedRequest.getRedirectUrl();
redirectStrategy.sendRedirect(request, response, targetUrl);
위 targetUrl 이 로그인 이전 요청 페이지가 이므로 위처럼 redirect 시키면 이전 페이지로 이동하게 된다.