简体   繁体   中英

Servlet response filter for non 2xx http code

I want to add some headers to http response from my server. I need to headers to be added regardless of the code(2xx, 4xx, 5xx) that returns the app. I tried to implement javax.servlet.Filter with @WebFilter annotation and javax.ws.rs.container.ContainerResponseFilter with @Provider annotation, but methods doFilter and filter called only when application returns 200 http status code. Is it possible to use filters for non 2xx responses?

I'm using Wildfly 8.2.0 as app server, but I think it's not important

EDIT

I think I should give more information about my problem. My application is REST-service implemented by Resteasy. Also I have configured security policy for authorization by using Wildlfy security subsystem. I need to response from REST-service always contains CORS headers for every request from frontend, even client doesn't pass authorization check. But when response code is 401, filter methods don't called, even if I use DispatcherType.ERROR , how suggest @Steve C . So, I don't return any status codes, all codes returns server (as say @ACV ). Maybe this is cause, that filters don't work for my case.

EDIT II

I found partial answer in this question . It's possible to add headers to all non-error (non 5xx) responses by configuring undertow subsystem at Wildfly standalone.xml

You need to include the DispatcherType in your @WebFilter declaration:

@WebFilter(urlPatterns={...}, dispatcherTypes={ERROR, REQUEST, ...})
public class ...

Try to implement a standard servlet filter. By the way. 404 comes from the server not from your application. Also 500. You can return these codes by yourself, in that case, the filter should work. Also, another solution would be to set the headers before any request. But this won't save you from the 404 problem. How to set servlet filters

I think you should try to capture errors through an error-page declaration in your 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>

... and then code my.jsp to process the response in case of error (I've always done exception processing with JSPs; I don't know if a servlet URI would work as well).

The only drawback is that you'll have to include explicitly an node for each HTTP error you want to process.

See http://docs.oracle.com/cd/E14571_01/web.1111/e13712/web_xml.htm#WBAPP537

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM