非 2xx http 代码的 Servlet 响应过滤器
Servlet response filter for non 2xx http code
我想向来自我的服务器的 http 响应添加一些 headers。无论 return 应用程序的代码(2xx、4xx、5xx)如何,我都需要添加 headers。我尝试使用 @WebFilter
注释实现 javax.servlet.Filter
并使用 @Provider
注释实现 javax.ws.rs.container.ContainerResponseFilter
,但是方法 doFilter
和 filter
仅在应用程序 [=46] 时调用=]s 200 http 状态代码。是否可以对非 2xx 响应使用过滤器?
我正在使用 Wildfly 8.2.0 作为应用服务器,但我认为这并不重要
编辑
我想我应该提供有关我的问题的更多信息。我的应用程序是 REST-service 由 Resteasy 实现的。我还使用 Wildlfy 安全子系统为授权配置了安全策略。对于来自前端的每个请求,我需要来自 REST-service 的响应始终包含 CORS headers,即使客户端未通过授权检查。但是当响应代码是 401 时,过滤方法不会被调用,即使我使用 DispatcherType.ERROR
,如何建议 @Steve C。
所以,我没有 return 任何状态代码,所有代码 returns 服务器(比如 @ACV)。也许这是因为过滤器不适用于我的情况。
编辑二
我在 this question 中找到了部分答案。通过在 Wildfly standalone.xml
配置 undertow 子系统,可以将 headers 添加到所有 non-error(非 5xx)响应
尝试实现标准的 servlet 过滤器。顺便一提。 404 来自服务器而不是您的应用程序。同样是 500。您可以自己 return 这些代码,在这种情况下,过滤器应该可以工作。此外,另一种解决方案是在任何请求之前设置 headers 。但这并不能使您免于 404 问题。
How to set servlet filters
您需要在您的@WebFilter 声明中包含 DispatcherType:
@WebFilter(urlPatterns={...}, dispatcherTypes={ERROR, REQUEST, ...})
public class ...
我认为您应该尝试通过 web.xml:
中的错误页面声明来捕获错误
<!--
The error-page element contains a mapping between an error code
or exception type to the path of a resource in the web application
-->
<error-page>
<!--
The error-code contains an HTTP error code, ex: 404
-->
<error-code></error-code>
<!--
The location element contains the location of the resource in the web
application relative to the root of the web application. The value of
the location must have a leading `/'.
-->
<location>/my.jsp</location>
</error-page>
... 然后编码 my.jsp 以在出现错误时处理响应(我一直使用 JSP 进行异常处理;我不知道 servlet URI 是否也能正常工作)。
唯一的缺点是您必须为要处理的每个 HTTP 错误显式包含一个节点。
见http://docs.oracle.com/cd/E14571_01/web.1111/e13712/web_xml.htm#WBAPP537
我想向来自我的服务器的 http 响应添加一些 headers。无论 return 应用程序的代码(2xx、4xx、5xx)如何,我都需要添加 headers。我尝试使用 @WebFilter
注释实现 javax.servlet.Filter
并使用 @Provider
注释实现 javax.ws.rs.container.ContainerResponseFilter
,但是方法 doFilter
和 filter
仅在应用程序 [=46] 时调用=]s 200 http 状态代码。是否可以对非 2xx 响应使用过滤器?
我正在使用 Wildfly 8.2.0 作为应用服务器,但我认为这并不重要
编辑
我想我应该提供有关我的问题的更多信息。我的应用程序是 REST-service 由 Resteasy 实现的。我还使用 Wildlfy 安全子系统为授权配置了安全策略。对于来自前端的每个请求,我需要来自 REST-service 的响应始终包含 CORS headers,即使客户端未通过授权检查。但是当响应代码是 401 时,过滤方法不会被调用,即使我使用 DispatcherType.ERROR
,如何建议 @Steve C。
所以,我没有 return 任何状态代码,所有代码 returns 服务器(比如 @ACV)。也许这是因为过滤器不适用于我的情况。
编辑二
我在 this question 中找到了部分答案。通过在 Wildfly standalone.xml
配置 undertow 子系统,可以将 headers 添加到所有 non-error(非 5xx)响应尝试实现标准的 servlet 过滤器。顺便一提。 404 来自服务器而不是您的应用程序。同样是 500。您可以自己 return 这些代码,在这种情况下,过滤器应该可以工作。此外,另一种解决方案是在任何请求之前设置 headers 。但这并不能使您免于 404 问题。 How to set servlet filters
您需要在您的@WebFilter 声明中包含 DispatcherType:
@WebFilter(urlPatterns={...}, dispatcherTypes={ERROR, REQUEST, ...})
public class ...
我认为您应该尝试通过 web.xml:
中的错误页面声明来捕获错误<!--
The error-page element contains a mapping between an error code
or exception type to the path of a resource in the web application
-->
<error-page>
<!--
The error-code contains an HTTP error code, ex: 404
-->
<error-code></error-code>
<!--
The location element contains the location of the resource in the web
application relative to the root of the web application. The value of
the location must have a leading `/'.
-->
<location>/my.jsp</location>
</error-page>
... 然后编码 my.jsp 以在出现错误时处理响应(我一直使用 JSP 进行异常处理;我不知道 servlet URI 是否也能正常工作)。
唯一的缺点是您必须为要处理的每个 HTTP 错误显式包含一个节点。
见http://docs.oracle.com/cd/E14571_01/web.1111/e13712/web_xml.htm#WBAPP537