public class LoginFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
String passUrl = "";
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
HttpServletRequest httpRequest=(HttpServletRequest)arg0;
HttpServletResponse httpResponse=(HttpServletResponse)arg1;
HttpSession session=httpRequest.getSession();
String[] strArray = passUrl.split(";");
//下边循环作用是依次检查访问的url中是否包含passurl中的关键字,如果包含则不进行拦截
for (String str : strArray) {
if (str.equals(""))//如果在上边配置中设置的url为空,则说明都需要进行拦截,跳过此循环
continue;
if (httpRequest.getRequestURL().indexOf(str) >= 0) {
arg2.doFilter(httpRequest, httpResponse);
return;
}
}
if(session.getAttribute("sessionUser")!=null){
arg2.doFilter(httpRequest, httpResponse);
}
else{
httpResponse.sendRedirect(httpRequest.getContextPath()+"/login.jsp");
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
passUrl = arg0.getInitParameter("passUrl");
}
}