본문 바로가기

Web

[Error]Handler 설정 실수로 인한 오류(Http 302 Found)

이미 로그인된 상태에서 도메인(예:http://localhost:8080)으로 접속하면 로그인페이지로 이동하는 이슈가 있어서 이를 수정하는 작업 중 발생한 오류에 대해서 적어볼까 합니다.

 

web.xml에서 도메인으로 접속하면 index.jsp으로 이동하도록 설정을 했습니다.

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

 

아래 index.jsp에서 세션이 없을 경우 로그인 페이지로 가도록 설정을 했습니다.

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" 	uri="http://java.sun.com/jsp/jstl/functions" %>
<% pageContext.setAttribute("crlf", "\n"); %>
<c:set var="context" value="${pageContext.request.contextPath}" />
<c:choose>
	<c:when test="${sessionScope.userVO != null && sessionScope.userVo != ''}">
		<jsp:forward page="${fn:replace(context,crlf,'/')}/main.do"/>		
	</c:when>
	<c:otherwise>
		<jsp:forward page="${fn:replace(context,crlf,'/')}/login.do"/>			
	</c:otherwise>
</c:choose>

 

Handler로직에서 제외시키기 위해 아래와 같이 interceptor 설정에서 로그인 페이지인 login.do를 제외시켰다.

    <mvc:interceptors>
    	<mvc:interceptor>
	        <mvc:mapping path="/**/*" />
	        <!-- <mvc:exclude-mapping path="/login.do" /> -->
	        <mvc:exclude-mapping path="/loginProcess.do" />
	        <mvc:exclude-mapping path="/logout.do" />
	        <bean name="commonInterceptor" class="framework.interceptor.CommonInterceptor" />
	    </mvc:interceptor>
    </mvc:interceptors>

 

위와 같이 설정을 수정하고 어플리케이션을 동작시키면 아래와 같이 오류가 발생합니다.

익스플로러 에러화면
익스플로러 개발자 도구에서의 네트워크 호출 상황
크롬 에러화면
크롬 개발자 도구에서의 네트워크 호출 상황

 

크롬 에러 화면 및 각 브라우저의 네트워크 현황 이력에서도 알 수 있듯이, /login.do로의 호출이 계속 반복되어서 브라우저가 스스로 동작을 중지하는 오류(Found 302)가 발생하였습니다.

 

위와 같은 오류의 원인을 확인해보니 interceptor에서 /login.do를 예외처리에서 제외시켰기 때문에 controller가 호출되기 전에 preHandle에서 계속 동일한 로직이 반복되었습니다. 처음에는 로그인을 하지 않아서 세션 정보가 없기 때문에 세션 정보를 확인하는 조건문에 계속 걸리는 것이 문제였습니다.

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	// 요청 URI
	String requestURI = request.getRequestURI();
	HttpSession session = request.getSession();
	UserVO userVO = (UserVO) session.getAttribute("userVO");
	// 세션 정보가 없으면 로그인 화면으로
	if(userVO == null) {
		response.sendRedirect("./login.do");
		return false;
	} else {
		// 세션 정보가 있지만 관리자가 아닌 경우는 terms 화면으로 
		if(requestURI.contains("/schedule.do") && "N".equals(userVO.getManagerYn())) {
			response.sendRedirect("/terms.do");
			return false;
		} else {
			return true;
		}
	}
	
}

 

그래서 아래와 같이 interceptor에서 /login.do 를 다시 예외처리에 추가해서 해결하였습니다.

    <mvc:interceptors>
    	<mvc:interceptor>
	        <mvc:mapping path="/**/*" />
	        <mvc:exclude-mapping path="/login.do" />
	        <mvc:exclude-mapping path="/loginProcess.do" />
	        <mvc:exclude-mapping path="/logout.do" />
	        <bean name="commonInterceptor" class="framework.interceptor.CommonInterceptor" />
	    </mvc:interceptor>
    </mvc:interceptors>