删除 Java 中的 HTTP 响应头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16339576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:41:40  来源:igfitidea点击:

Remove HTTP Response headers in Java

javahttp-headersweblogic-10.x

提问by To Kra

Is there a way to remove HTTP response headers like Server and X-Powered-By?

有没有办法删除像 Server 和 X-Powered-By 这样的 HTTP 响应标头?

My application is using a Weblogic server. I'm programming in Java using the Spring MVC framework and Hibernate, and using JSP for views.

我的应用程序正在使用 Weblogic 服务器。我正在使用 Spring MVC 框架和 Hibernate 在 Java 中编程,并使用 JSP 进行视图。

回答by Peter Bratton

Depends on where the headers are added. If inside your app, you can use a Spring MVC Interceptorto remove them after your controller calls. If outside your app, you might be able to try a Java EE filterconfigured in web.xml (the example is security, but the approach will also work for your use case). If its added after that, you may want to look at your web front end (Apache, IIS, what-have-you) to configure a filter there.

取决于添加标题的位置。如果在您的应用程序中,您可以在控制器调用后使用Spring MVC 拦截器删除它们。如果在您的应用程序之外,您也许可以尝试在 web.xml 中配置的Java EE 过滤器(示例是安全性,但该方法也适用于您的用例)。如果它是在此之后添加的,您可能需要查看您的 Web 前端(Apache、IIS、what-have-you)以在那里配置过滤器。

UPDATE

更新

This answerdescribes an approach for removing specific headers, as the HttpServletResponse interface does not allow for header removal explicitly. You will need some trial and error to determine what portion of your stack is adding the header.

此答案描述了一种删除特定标头的方法,因为 HttpServletResponse 接口不允许显式删除标头。您需要反复试验才能确定堆栈的哪一部分添加了标头。

回答by To Kra

If you set concrete responseHeader to nullit will be removed / not seen in response headers.

如果您将具体的 responseHeader 设置为null它将被删除/在响应标头中看不到。