【问题标题】:Spring Servlet Filter causing browser connection to be resetSpring Servlet过滤器导致浏览器连接被重置
【发布时间】:2017-01-17 20:54:55
【问题描述】:

我有一个 Spring web 应用程序(暂时)只有一个配置为拦截所有请求的 servlet 过滤器(没有控制器/方法等):

// Groovy pseudo-code, but that shouldn't matter, all the logic is there
// and we know it works (no compiler errors) because I can go to localhost:8080
// in a browser and see it.
@Override
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    println 'I am being invoked to filter the response!'

    HttpServletResponse httpServletResponse = response as HttpServletResponse
    String htmlResponse = """
    <html>
    <head><title>Hello!</title></head>
    <body>
        Hello from filter land!
    </body>
    </html>
    """

    httpServletResponse.writer.write(htmlResponse)
    httpServletResponse.writer.flush()
}

当我启动应用程序并将浏览器指向http://localhost:8080 时,我得到了预期的 HTML(一条消息,内容为“Hello from filter land!”)。我还看到println 出现在我的控制台输出中。

然后我通过转到Preferences &gt;&gt; Network 并使用以下配置来调整我的浏览器 (FireFox) 以将此代理用于所有 HTTP 流量:

我点击OK 然后转到http://example.com 并收到“连接已重置”错误。在Web Developer &gt;&gt; Network 工具中,也没有太多信息。只是基本上表明连接不好:

还需要注意的是,当我通过 FireFox 访问 http://example.com(或任何其他 HTTP 站点)时,我没有看到 println 打印到控制台输出。很明显,浏览器似乎没有正确“命中”过滤器。

关于为什么我的 servlet 过滤器对http://localhost:8080 起作用的任何想法,但在我的浏览器被代理使用它时却没有?尝试首先排除我使用 servlet 过滤器和/或 Spring 的方式是否是应用层问题。

【问题讨论】:

  • 您究竟是从哪里学会使用这样的过滤器的?我从未在任何自尊的学习资源中看到过这样的“Hello World”过滤器。过滤器不是 HTTP 端点。您的过滤器基本上是在等待指令,以便继续向所需的 HTTP 端点(例如 servlet 实例、JSP 文件、HTML 文件等)发出请求。请改用 HTTP servlet。顺便说一句,servlet 和过滤器都与 Spring 无关。如果您更喜欢 Spring 而不是 Java EE,只需使用 Spring 控制器而不是 HTTP servlet。
  • 我创建了完全相同的代理过滤器并使用相同的 Firefox 代理设置运行它,当我转到 example.com 时,我没有收到您在 Firefox、Chrome 或 Safari 中看到的错误.我正在使用 Spring CLI 应用程序来测试 groovy 过滤器作为 Spring Boot 应用程序的唯一 @Component。你用什么做应用服务器?它没有打印到控制台的事实告诉我问题可能不在应用程序代码中。

标签: spring firefox proxy servlet-filters


【解决方案1】:

您需要先运行网络代理,然后才能设置代理。像这样http://squidman.net/squidman/

【讨论】:

    猜你喜欢
    • 2011-03-23
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2011-12-21
    • 1970-01-01
    • 2012-02-15
    • 2017-02-21
    相关资源
    最近更新 更多