如何在 Jetty 9.2.x 中删除 "Powered By"

How to remove "Powered By" in Jetty 9.2.x

我正在尝试实现自定义错误处理程序并在 /webapp/WEB-INF 中使用 jetty-web.xml,它工作正常。

<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
   <Set name="errorHandler">
      <New class="com.example.CustomErrorHandler" />
   </Set>
</Configure> 

但我需要将此处理程序放在 /etc/jetty.xml 中,以使其对整个服务器可用。我试过像doku中提到的那样:https://www.eclipse.org/jetty/documentation/current/custom-error-pages.html

<Configure id="Server" class="org.eclipse.jetty.server.Server">
...
  <Call name="addBean">
    <Arg>
      <New class="com.example.CustomErrorHandler"/>
    </Arg>
  </Call> 
</Configure>

这不会引发任何错误,服务器工作正常,但默认错误处理程序而不是自定义处理程序处理错误页面。

这是自定义错误处理程序:

public class CustomErrorHandler extends ErrorHandler {

   public CustomErrorHandler() {
      super.setShowMessageInTitle(false);
      super.setShowStacks(false);
   }

   @Override
   protected void writeErrorPageBody(final HttpServletRequest request, final Writer writer, final int code,
         final String message, final boolean showStacks) throws IOException {
      final String uri = request.getRequestURI();

      writeErrorPageMessage(request, writer, code, message, uri);
      if (showStacks) {
         writeErrorPageStacks(request, writer);
      }
   }
}

那么为什么自定义处理程序没有在 jetty.xml 中激活?

编辑:使用码头 9.2.19.v20160908

Note: Jetty 9.2.x is EOL (End of Life) - Use a newer supported / stable version of Jetty. https://www.eclipse.org/jetty/documentation/current/what-jetty-version.html

存在从默认错误页面中删除 Powered by Jetty 的配置(以及从 HTTP 响应 header 中删除 Server header)。

这是 HttpConfiguration.setSendServerVersion(false) 设置。

无需创建自定义 ErrorHandler 或任何复杂的东西。

HttpConfiguration 位于 ServerConnector 中,该 ServerConnector 被设置为绑定到一个端口并接受您的传入连接。

如果您在 ${jetty.home} (as documented) 中使用正确的 ${jetty.base} 目录,那么您可以使用 属性 为所有 ServerConnector ${jetty.home} 附带的配置 ...

一个例子。

让我们设置一个简单的 ${jetty.base} 足以演示。

[tmp]$ mkdir noprovided-base
[tmp]$ cd noprovided-base/
[noprovided-base]$ java -jar ~/jetty-home-9.4.22.v20191022/start.jar \
  --create-startd \
  --add-to-start=http,deploy,webapp
MKDIR : ${jetty.base}/start.d
INFO  : webapp          initialized in ${jetty.base}/start.d/webapp.ini
INFO  : server          transitively enabled, ini template available with --add-to-start=server
INFO  : security        transitively enabled
INFO  : servlet         transitively enabled
INFO  : http            initialized in ${jetty.base}/start.d/http.ini
INFO  : threadpool      transitively enabled, ini template available with --add-to-start=threadpool
INFO  : bytebufferpool  transitively enabled, ini template available with --add-to-start=bytebufferpool
INFO  : deploy          initialized in ${jetty.base}/start.d/deploy.ini
MKDIR : ${jetty.base}/webapps
INFO  : Base directory was modified

[noprovided-base]$ ls -la
total 16
drwxr-xr-x  4 joakim joakim 4096 Nov 13 08:09 ./
drwxr-xr-x 13 joakim joakim 4096 Nov 13 08:08 ../
drwxr-xr-x  2 joakim joakim 4096 Nov 13 08:09 start.d/
drwxr-xr-x  2 joakim joakim 4096 Nov 13 08:09 webapps/

现在让 运行 这基本上是默认的/未配置的 ${jetty.base}

[noprovided-base]$ java -jar ~/jetty-home-9.4.22.v20191022/start.jar
2019-11-13 08:12:28.248:INFO::main: Logging initialized @379ms to org.eclipse.jetty.util.log.StdErrLog
2019-11-13 08:12:28.467:INFO:oejs.Server:main: jetty-9.4.22.v20191022; built: 2019-10-22T13:37:13.455Z; git: b1e6b55512e008f7fbdf1cbea4ff8a6446d1073b; jvm 11.0.5+10
2019-11-13 08:12:28.480:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///home/joakim/tmp/noprovided-base/webapps/] at interval 1
2019-11-13 08:12:28.503:INFO:oejs.AbstractConnector:main: Started ServerConnector@48ebde5e{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2019-11-13 08:12:28.504:INFO:oejs.Server:main: Started @634ms

并查看它产生什么样的错误消息(和响应 headers)...

[tmp]$ curl -vvvv http://localhost:8080/flarg
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /flarg HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Type: text/html;charset=iso-8859-1
< Content-Length: 442
< Server: Jetty(9.4.22.v20191022)
< 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404 Not Found</h2>
<table>
<tr><th>URI:</th><td>/flarg</td></tr>
<tr><th>STATUS:</th><td>404</td></tr>
<tr><th>MESSAGE:</th><td>Not Found</td></tr>
<tr><th>SERVLET:</th><td>-</td></tr>
</table>
<hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.22.v20191022</a><hr/>

</body>
</html>

是的,Powered by Jetty:// 9.4.22.v20191022 响应 body 页脚和 Server: Jetty(9.4.22.v20191022) header 存在。

现在让我们配置此 ${jetty.base} 以禁用版本报告。

[noprovided-base]$ echo "jetty.httpConfig.sendServerVersion=false" >> start.ini
[noprovided-base]$ cat start.ini 
jetty.httpConfig.sendServerVersion=false

一次重新启动服务器,另一次测试显示...

[tmp]$ curl -vvvv http://localhost:8080/flarg
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /flarg HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Type: text/html;charset=iso-8859-1
< Content-Length: 357
< 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404 Not Found</h2>
<table>
<tr><th>URI:</th><td>/flarg</td></tr>
<tr><th>STATUS:</th><td>404</td></tr>
<tr><th>MESSAGE:</th><td>Not Found</td></tr>
<tr><th>SERVLET:</th><td>-</td></tr>
</table>

</body>
</html>

Powered by 响应 body 页脚和 Server 响应 header 现在消失了。

在您的 Jetty 版本 (9.2.18.v20160721) 中,"Powered by Jetty" 存在于 5 个不同的位置(ContextHandlerDefaultHandlerHttpSpiContextHandlerResponse,最后是 ErrorHandler)。如果您使用的是独立 Jetty 服务器,则始终存在 4 个。

使用旧版本的 Jetty 无法删除所有这些引用。

此问题已通过 Jetty 9.3.0 代码库的多次提交得到解决。

最终提交是 eclipse/jetty.project@64287189

您必须使用 Jetty 9.3.0(或更新版本)才能从 Jetty 生成的响应类型中删除这些引用。