package com.accp.epet.common;
import java.io.IOException;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginFilter extends HttpServlet implements Filter {
private final static String[] exact_url = { "index.jsp", "styles.css",
"/images" ,"login.do" }; // 不用做权限判断的URL
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
boolean needFilt = false;//是否需要过滤
String uri=req.getRequestURI();
//.jsp和.do的uri需要过滤
if (req.getRequestURI().indexOf(".jsp") >= 0
|| req.getRequestURI().indexOf(".do") >= 0)
needFilt = true;
//不用做权限判断的URL不需要过滤
for (int i = 0; i < exact_url.length; i++) {
if (req.getRequestURI().indexOf(exact_url[i]) >= 0) {
needFilt = false;
break;
}
}
if (needFilt && req.getSession().getAttribute("login") == null) {
res.sendRedirect(req.getContextPath() + "/index.jsp");
return;
}
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
}
}