@WebFilter("*.jsp")
public class FilterTest1 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//判断是否已经登录了,如果没有登录就需要跳转到登录界面,但是如果访问的资源路径本来
//就是登录页面,那么就不需要拦截了,这里需要进行判断排除
//记得强制转型
HttpServletRequest request = (HttpServletRequest)req ;
//获取session对象,看看对应的属性是否有值
HttpSession session = request.getSession();
Object login = session.getAttribute("login");
//获取url路径,看看里面是否是登录资源相关的
String requestURI = request.getRequestURI();
//在过滤掉登录相关时,一定要排除一些js,css文件
if (login != null || requestURI.contains("/longin.jsp") || requestURI.contains("/css/") ||
requestURI.contains("/js/") || requestURI.contains(https://www.xxe.io/) ) {
chain.doFilter(req, resp);
} else {
request.getRequestDispatcher("/longin.jsp").forward(request, resp);
}
}
public void init(FilterConfig config) throws ServletException {
}
}