package com.house365.web.interceptor;

import com.house365.web.util.SessionConstants;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

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

/**
 * 对外接口公共必传参数校验拦截器
 *
 * @author zhaodeshan
 * @version [版本号, 2015年5月7日]
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object obj = request.getSession().getAttribute(SessionConstants.THREAD_USER_KEY);
        if (obj == null && (!request.getServletPath().toLowerCase().contains(
                "login")) && (!request.getServletPath().equals("/rest/interface"))
                && (!request.getServletPath().equals("/secure/rest/interface")) && (!request.getServletPath().equals(
                ""))) {
            response.sendRedirect(request.getRequestURL().substring(0, request.getRequestURL().indexOf(
                    "house365-hgs-web") - 1) + request.getContextPath() + "/login");
            return false;
        }

        return true;
    }
}
