package com.yc.education.interceptor;


import com.yc.education.model.Admins;
import com.yc.education.util.AppConst;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

public class GlobalInterceptor extends HandlerInterceptorAdapter {



	@Override  
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Admins admins = (Admins) session.getAttribute(AppConst.Session_Admin);
        if(admins!=null){
            return true;
        }else{

            if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ //如果是ajax请求响应头会有x-requested-with
                PrintWriter out = response.getWriter();
                out.print("loseSession");//session失效
                out.flush();
                return false;
            }

            String contextPath=request.getContextPath();
            response.sendRedirect(contextPath + "/admin/login.html");
            return false;
        }


	}


    
}  