[英]Infinite loop when using filter for jsp files
当我为所有jsp页面创建过滤器时,浏览器进入无限循环,但是当我仅为一页创建过滤器时,浏览器将正确运行!
这是doFilter方法,如果有人发现错误plx告诉我...
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (debug) log("AuthenticationFilter:doFilter()");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpServletResponse httpres = (HttpServletResponse) response;
HttpServletRequest httpreq = (HttpServletRequest) request;
if (httpreq.getRequestURI().indexOf("login.jsp") == -1 || httpreq.getRequestURI().indexOf("LoginServlet") == -1) {
// if(!httpreq.getRequestURL().equals("/OSQS/Login.jsp")){
HttpSession session = httpreq.getSession();
String logged = (String) session.getAttribute("login");
if (logged == null) {
httpres.sendRedirect("login.jsp");
return;
}
}
chain.doFilter(request, response);
}
造成此问题的原因是,过滤器的url-pattern
显然过于通用,例如/*
或*.jsp
。 它将在每个 JSP请求上执行。
在过滤器中,当没有登录用户时,您将重定向到login.jsp
。 重定向将指示客户端触发新的HTTP请求。 当请求URL与其url-pattern
匹配时,新的HTTP请求将再次调用过滤器。 因为仍然没有登录的用户,所以它会进入无限重定向循环。
除了确定请求网址(如您所做的那样)之外,您还可以将受保护的页面放在更具体的url-pattern
,例如/secured/*
, /private/*
左右,然后将受保护的页面放在此处,但是登录页面没有。 如果您重定向到登录页面,则不会再调用该过滤器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.