如何删除HTTP响应头?
我遇到一种情况,必须删除其中一个响应标头 Content-Disposition
。所以我想到编写一个servlet过滤器来做到这一点。但我意识到 HttpServletResponse 只有一个 setHeader() 方法,但没有删除它的方法。 我该怎么做?
I have a situation where one of the response headers Content-Disposition
has to be removed. So I thought of writing a servlet filter to do this. But I realized that the HttpServletResponse
has only a setHeader()
method but no method to remove it.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
之后您无法通过标准 Servlet API 删除标头。最好的办法是阻止设置标头。您可以通过创建一个
Filter
来替换ServletResponse 与自定义
HttpServletResponseWrapper
实现跳过setHeader()
每当标题名称为内容处置
。基本上:
只需将该过滤器映射到感兴趣的 URL 模式即可运行。
You can't delete headers afterwards by the standard Servlet API. Your best bet is to just prevent the header from being set. You can do this by creating a
Filter
which replaces theServletResponse
with a customHttpServletResponseWrapper
implementation which skips thesetHeader()
's job whenever the header name isContent-Disposition
.Basically:
Just map that filter on the URL-pattern of interest to get it to run.
这可能不符合 Servlet API,但将该值设置为 null 可以在 GlassFish 4 上工作,也可能在 Tomcat 上工作,因为这是 GlassFish 下的内容。
我们确实需要更新 Servlet API 规范以添加允许删除标头的方法或正式支持使用带有 null 值的 setHeader。
这一点很重要的一个例子是,如果您在 Web 应用程序上使用安全约束 (SSL/TLS),则静态资源缓存会变得复杂,因为容器会自动添加标头以防止缓存(您可以尝试使用 disableProxyCaching 和Tomcat/GlassFish 上的 securePagesWithPragma)。我已经有了一个用于缓存控制的 servlet 过滤器,它非常适合非安全内容,因此我想将缓存控制全部保留在一个位置,只需将 Prama 和 Cache-Control 设置为 null 即可清除任何容器添加的标头。
This may not be Servlet API compliant, but setting the value to null works on GlassFish 4 and probably on Tomcat too as that is what is underneath GlassFish.
We really need to update the Servlet API specification to either add a method to allow removing headers or to officially support using setHeader with a null value.
An example where this is important is if you use a security constraint (SSL/TLS) on your web application then static resource caching is complicated by the fact that the container will automatically add headers to prevent caching (you can try to disable with disableProxyCaching and securePagesWithPragma on Tomcat/GlassFish). I've already got a servlet filter for cache control that works great for non-secure content so I would like to keep cache control all in one place and simply set Prama and Cache-Control to null to clear any container added headers.
正如其他回应。设置后无法删除标头,至少不是标准的(glassfish 可以清除标头,将其值设置为 null)。因此,最终您将有两个选择:
使用response.reset()重置响应 - 这会有效地删除所有标头以及任何缓冲数据,具体取决于您的情况一个很好的选择(在我的例子中是在身份验证验证错误之后)。如果响应已经提交,您将收到 IllegalStateException。
将标头设置为空字符串,显然这不会删除标头。但是http规范只有Accept-Encoding、TE(传输编码)和HOST标头中的一些定义和空值,因此根据您的需要,您可以在应用程序层中对其进行控制。
As the other responses. There is no way to remove a header after being set, at least not standard (glassfish lets clear a header setting it's value to null). So at the end of the day you would have two choices:
Reset the response with
response.reset()
- This effectively removes ALL headers AND ALSO ANY BUFFERED DATA, depending on you case can be a good alternative (in my case was after authentication validation errors). If the response is already committed you'll get an IllegalStateException.Set header to empty string, clearly this doesn't remove the header. But the http specification only has some definitions for and empty value in the Accept-Encoding, TE (transfer encoding) and HOST headers, so depending in your needs you can control that in your application layer.
这对使用 Spring 4 的我不起作用。我正在尝试删除 Expires 响应标头。对于每一页。像这样:
下面是我添加过滤器的方法:
为 Expires 和 Cache-Control 调用 setHeader(),但我无法覆盖 Expires 过滤器值或 Cache-Control 值。我可以添加 Cache-Control 值。如果我在 Cache-Control 上调用 setHeader,它就会变成一个值数组。但我需要删除标题。
This does not work for me using Spring 4. I'm trying to strip out the Expires response header. For every page. Like so:
And here is how I add the filter:
setHeader() being called for Expires and Cache-Control, but I can't override the Expires filter value, or the Cache-Control value. I can add to the Cache-Control value. It turns into an array of values if I call setHeader on Cache-Control. But I need to delete the header.