【问题标题】:filterChain.doFilter is not moving to next filterfilterChain.doFilter 没有移动到下一个过滤器
【发布时间】:2020-11-08 02:44:53
【问题描述】:

这是我的过滤器代码

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
    
    String path = ((HttpServletRequest) request).getRequestURI();
    LOGGER.log(Level.INFO, "Current request path :: {0}", path);
    if (ignorePath(path)) {
        LOGGER.log(Level.INFO, "continuing to next filter");
        chain.doFilter(request, response);
    }
    LOGGER.log(Level.INFO, "Should not come here");

}

日志记录仅用于调试目的。 我正在执行以下日志。

2020-07-18T12:02:30.805301+00:00 app[web.1]:2020 年 7 月 18 日下午 12:02:30 security.filter.SecurityFilter doFilter

2020-07-18T12:02:30.805302+00:00 app[web.1]:INFO:继续下一个过滤器

2020-07-18T12:02:30.889518+00:00 app[web.1]:2020 年 7 月 18 日 12:02:30 PM security.filter.SecurityFilter doFilter

2020-07-18T12:02:30.889520+00:00 app[web.1]:信息:不应该来这里

这里到底出了什么问题?日志不应该来这里,不应该被打印(这是我所期望的)。

【问题讨论】:

    标签: servlets jax-rs servlet-filters


    【解决方案1】:

    每次通过链传递请求/响应时都会调用doFilter()FilterChain.doFilter() 方法调用链中的下一个过滤器,如果过滤器是链中的最后一个过滤器,则调用链末尾的资源。

    这只是一个普通的方法,在它执行之后,控件将进入下一行。这就是您在 if 条件之后获取日志的原因。

    如果您想忽略某些 URI 的下一个过滤器的执行,我建议您使用 else 块。

    if (ignorePath(path)) {
        chain.doFilter(request, response);
        LOGGER.log(Level.INFO, "Called doFilter for this URI");
    } else {
        LOGGER.log(Level.INFO, "doFilter is not called for this URI");
    }
    

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 2015-07-20
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      相关资源
      最近更新 更多