package cn.hmsoft.application.web;

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

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.hmsoft.exception.web.LoginTimeoutException;

/********************
 * 默认拦截器，主要处理登录超时的问题
 * 
 * @author zxq
 * @create 2019-05-06 08:07:31
 * @version 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public class DefaultInterceptor extends HandlerInterceptorAdapter {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		AjaxControl control = null;
		// 允许跨域访问
		response.setHeader("Access-Control-Allow-Origin", "*");
		try {
			HandlerMethod method = (HandlerMethod) handler;
			control = (AjaxControl) method.getBean();
		} catch (Exception e) {
			control = null;
		}
		if (control == null)
			return super.preHandle(request, response, handler);

		if (control.validRole() != null) {
			// request.getSession().setMaxInactiveInterval(AppHelper.SessionTimeOutSecond);
			return super.preHandle(request, response, handler);
		} else {
			// 丢失登录信息
			throw new LoginTimeoutException();
		}
		// return true;

	}
}