package com.uas.sso.web.filter;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uas.sso.SSOConfig;
import com.uas.sso.SSOHelper;
import com.uas.sso.Token;
import com.uas.sso.common.util.HttpUtil;

/**
 * SSO 过滤器验证登录状态
 */
public class SSOFilter implements Filter {

	private static final Logger logger = Logger.getLogger(SSOFilter.class.getName());
	private static String OVERURL = null;

	public void init(FilterConfig config) throws ServletException {
		/**
		 * 从应用 web.xml 配置参数中 获取不需要拦截URL
		 */
		OVERURL = config.getInitParameter("over.url");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		boolean isOver = HttpUtil.inContainURL(req, OVERURL);
		/** 非拦截URL、安全校验Cookie */
		if (!isOver) {
			Token token = SSOHelper.getToken(req);
			if (token == null) {
				/**
				 * 重新登录
				 */
				logger.fine("logout. request url:" + req.getRequestURL());
				SSOHelper.clearRedirectLogin(req, res);
				return;
			} else {
				req.setAttribute(SSOConfig.SSO_TOKEN_ATTR, token);
			}
		}
		chain.doFilter(request, response);
	}

	public void destroy() {
		OVERURL = null;
	}
}
