'How to add HTML-Payload for all redirections via Location-Header

Is it possible to intercept all responses with redirection via location header to enrich the payload with HTML meta refresh or redirection via JS?



Solution 1:[1]

Current Solution that works for me and is used during authentication redirection:

Configure a Filter after LogoutFilter.class. This Filter wraps HttpServletResponse. This wrapper of HttpServletResponse overrides HttpServletResponse#sendRedirect(String) to print to response writer and HttpServletResponse#setHeader(String) to also print to response writer -- in case someone didn't use #sendRedirect(String) but set Location header.

The Reason to override #sendRedirect(String) ist that the response stream is reset before at the beginn and closes the stream at the end.

No embedded implementation provides by default a payload for redirection. Tomcat can use 'coyoteResponse.sendRedirect.note' property and print it in redirect. Jetty and Undertow do not print to response writer any payload.

Note: Not using Location header to redirect slows website response and IMHO makes a horrible user experience.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul