package org.bench4q.web.security;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

public class SessionFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		String[] notFilter = new String[] { "/index", "/css/", "/script/",
				"/i18n/", "/images/", "/publiccontrol/", "/login","/lib/", "/js", "/register","/adminLogin"};

		// 请求的uri
		String uri = request.getRequestURI();
		String user = (String) request.getSession().getAttribute("username");
		if (filterChain == null) {
			return;
		}
		if (user == null || user.equals("")) {
			// uri中包含background时才进行过滤
			for (String s : notFilter) {
				if (uri.contains(s)) {
					filterChain.doFilter(request, response);
					return;
				}
			}
			if (uri.equals("/")) {
				filterChain.doFilter(request, response);
				return;
			} else {
				response.sendRedirect("index.jsp");
				return;
			}
		} else {
			filterChain.doFilter(request, response);
			return;
		}
	}
}
