Framework2014. 12. 10. 10:51

servlet-context.xml 설정 추가

    
    <interceptors>
        <interceptor>
            <mapping path="/**/*" />
            <exclude-mapping path="/main/*"/>
            <exclude-mapping path="/login/*"/>
            <beans:bean class="com.d4emon.interceptor.SessionInterceptor"></beans:bean>            
        </interceptor>        
    </interceptors>
    
    

*** exclude-maping 은 Spring 3.2 부터 지원한다. ****

SessionInterceptor.java 파일 생성

 

package com.d4emon.interceptor;

 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;  
 import javax.servlet.http.HttpSession;

 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  
 public class SessionInterceptor extends HandlerInterceptorAdapter {
  
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

   System.out.println("Interceptor : PreHandle");
   
   // Session userid check
   HttpSession session = request.getSession();   
   String userid = (String) session.getAttribute("userid");

   // Login false
   if(null==userid) {
    System.out.println("Interceptor : Session Check Fail");
    // main page 로 이동
    response.sendRedirect("/main/main.do");
    return false;
   } 
   // Login true
   else { 
    System.out.println("Interceptor : Session Check true");
    return super.preHandle(request, response, handler);
   }
  }
 }

Posted by 비니미니파파