【问题标题】:How to remove "Powered By" in Jetty 9.2.x如何在 Jetty 9.2.x 中删除“Powered By”
【发布时间】:2019-11-13 09:31:04
【问题描述】:

我正在尝试实现一个自定义错误处理程序并在 /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

【问题讨论】:

  • 我看到提到了jetty-web.xml/webapp/WEB-INF,这是针对实际的 WAR / WebApp 的吗?还是别的什么?
  • 另外,你的最终目标是什么,你想要达到什么目标? (自定义ErrorHandler 通常适用于那些想要管理非上下文错误的人,这与您通过jetty-web.xml 使用它的努力相反)
  • 我的目标是从默认错误页面中删除“Powered by Jetty”。
  • 是的,jetty-web.xml 使用 cutsom 错误处理程序调用 jar 并将其提供给上下文处理程序

标签: java jetty


【解决方案1】:

注意:Jetty 9.2.x 是 EOL(生命终止)- 使用更新的受支持/稳定版本的 Jetty。 https://www.eclipse.org/jetty/documentation/current/what-jetty-version.html

有一个配置可以从默认错误页面中删除 Powered by Jetty(以及 HTTP 响应标头中的 Server 标头)。

这是HttpConfiguration.setSendServerVersion(false) 设置。

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

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

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

一个例子。

让我们设置一个简单的${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

看看它会产生什么样的错误信息(和响应头)......

[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 响应正文页脚和 Server: Jetty(9.4.22.v20191022) 标头存在。

现在让我们配置这个${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 响应正文页脚和 Server 响应标头现已消失。

在您的 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 生成的响应类型中删除这些引用。

【讨论】:

  • 感谢您的详细解答。我在 etc/jetty.xml 中试过这个: ... falsefalse 但还是不行,显示“由码头供电”。
  • 您升级了 Jetty 版本了吗?
  • 由于您正在更改不显示 Jetty 的版本,大概是为了某种安全审计。您应该知道 Jetty 9.2.19.v20160908 容易受到多个 CVE 的攻击。您需要升级以满足任何合法的安全审核。
  • 遗憾的是,目前无法进行更新。难道我现在的版本不能解决这个“由码头供电”的问题吗?
  • 如果您的目标是从自动生成的 Jetty 响应(标题和正文)中删除“Powered by”,那么您必须更新,因为有更多的地方可以生成“Powered by”然后只是 @ 987654352@,其他地方不能用自定义代码覆盖。查看更新的答案。
猜你喜欢
  • 2011-02-09
  • 2012-05-29
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 2023-02-01
相关资源
最近更新 更多