如何用阀门修改Tomcat的响应头? [英] How to modify Tomcat's response header with valves?

查看:292
本文介绍了如何用阀门修改Tomcat的响应头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要最小化Tomcat的响应标头.发送请求到tomcat的设备的内存非常有限,因此我想删除标头Date,Server和Content-Type. 我用动作钩设置了自己的阀门.该挂钩被调用,但似乎无法删除日期和服务器标头.

I need to minimize Tomcat's response header. The device sending requests to tomcat is very limited in memory, so I want to remove the headers Date, Server and Content-Type. I set up my own valve with an action hook. The hook gets called, but it seems to be impossible to remove the date and server header.

首先,我仅在ActionCode == COMMIT无效时才执行我的方法. 奇怪的.因此,我查看了Tomcat源代码:在Tomcat AbstractHttp11Processor的action()方法中,ActionCode==COMMIT

First I executed my method only when ActionCode == COMMIT - didn't work. Strange. So I looked in the Tomcat source code: In the method action() of Tomcat's AbstractHttp11Processor when ActionCode==COMMIT

  • prepareResponse()被调用(设置服务器和日期标头),然后
  • 它将标头写入输出缓冲区.
  • prepareResponse() is called (which set's the server and date headers) and then
  • it writes the headers to the output buffer.

因此无法拦截.当我在调用此操作之前设置标题时,它们会在AbstractHttp11Processor中被覆盖.当我在此之后设置标头时,它们将被忽略,因为它们已被写入缓冲区.我唯一的机会似乎是重置输出缓冲区并将标头写入缓冲区,还是有更好的方法?甚至有可能做到这一点?

So it is not possible to intercept. When I set the headers before this action gets called they get overwritten in the AbstractHttp11Processor. When I set the headers after that, they will be ignored, as they were already written to the buffer. My only chance seems to be to reset the output buffer and write the headers to the buffer or is there a better way? And is it even possible to do that?

推荐答案

日期标头无法删除. HTTP 1.1规范要求它.参见 RFC2626,第14.18节.

The date header cannot be removed. It is required by the HTTP 1.1 spec. See RFC2626, section 14.18.

目前无法禁用服务器标头,但是如果您针对Tomcat打开增强请求,我会考虑添加一个选项来完全禁用它.

There is currently no way to disable the server header but if you open an enhancement request against Tomcat I'll look into adding an option to disable it completely.

这篇关于如何用阀门修改Tomcat的响应头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆