注意: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 个不同的地方(ContextHandler、DefaultHandler、HttpSpiContextHandler、Response,最后是 ErrorHandler )。如果您使用的是独立的 Jetty 服务器,则始终存在 4 个。
旧版本的 Jetty 无法删除所有这些引用。
已通过 Jetty 9.3.0 代码库上的多次提交解决此问题。
最后的提交是eclipse/jetty.project@64287189
您必须使用 Jetty 9.3.0(或更高版本)才能从 Jetty 生成的响应类型中删除这些引用。