简体   繁体   English

对JSP文件使用过滤器时出现无限循环

[英]Infinite loop when using filter for jsp files

When I make filter for all jsp pages, the browser goes into an infinite loop, but when I make filter for only one page, it runs correctly!! 当我为所有jsp页面创建过滤器时,浏览器进入无限循环,但是当我仅为一页创建过滤器时,浏览器将正确运行!

Here is doFilter method, if some one find the error plx tell me... 这是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);

}

The cause of this problem is that the filter's url-pattern is apparently too generic, eg /* or maybe *.jsp . 造成此问题的原因是,过滤器的url-pattern显然过于通用,例如/**.jsp It will be executed on every JSP request. 它将在每个 JSP请求上执行。

In the filter you're sending a redirect to login.jsp when the logged in user is absent. 在过滤器中,当没有登录用户时,您将重定向到login.jsp A redirect will instruct the client to fire a new HTTP request. 重定向将指示客户端触发新的HTTP请求。 A new HTTP request will invoke the filter again when the request URL matches its url-pattern . 当请求URL与其url-pattern匹配时,新的HTTP请求将再次调用过滤器。 Because the logged in user is still absent, it goes into an infinite redirect loop. 因为仍然没有登录的用户,所以它会进入无限重定向循环。

Apart from determining the request URL (as you did), you can also just place the secured pages on a more specific url-pattern , eg /secured/* , /private/* or so and then place the secured pages there, but the login page not. 除了确定请求网址(如您所做的那样)之外,您还可以将受保护的页面放在更具体的url-pattern ,例如/secured/*/private/*左右,然后将受保护的页面放在此处,但是登录页面没有。 If you redirect to the login page, then the filter won't be invoked more. 如果您重定向到登录页面,则不会再调用该过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM