Class RelativeRedirectFilter

All Implemented Interfaces:
jakarta.servlet.Filter, Aware, BeanNameAware, DisposableBean, InitializingBean, EnvironmentAware, EnvironmentCapable, ServletContextAware

public class RelativeRedirectFilter extends OncePerRequestFilter
Overrides HttpServletResponse.sendRedirect(String) and handles it by setting the HTTP status and "Location" headers, which keeps the Servlet container from re-writing relative redirect URLs into absolute ones. Servlet containers are required to do that but against the recommendation of RFC 7231 Section 7.1.2, and furthermore not necessarily taking into account "X-Forwarded" headers.

Note: While relative redirects are recommended in the RFC, under some configurations with reverse proxies they may not work.

Since:
4.3.10
Author:
Rob Winch, Rossen Stoyanchev
  • Constructor Details

    • RelativeRedirectFilter

      public RelativeRedirectFilter()
  • Method Details

    • setRedirectStatus

      public void setRedirectStatus(HttpStatusCode status)
      Set the default HTTP Status to use for redirects.

      By default this is HttpStatus.SEE_OTHER.

      Parameters:
      status - the 3xx redirect status to use
    • getRedirectStatus

      public HttpStatusCode getRedirectStatus()
      Return the configured redirect status.
    • doFilterInternal

      protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException
      Description copied from class: OncePerRequestFilter
      Same contract as for doFilter, but guaranteed to be just invoked once per request within a single request thread. See OncePerRequestFilter.shouldNotFilterAsyncDispatch() for details.

      Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.

      Specified by:
      doFilterInternal in class OncePerRequestFilter
      Throws:
      jakarta.servlet.ServletException
      IOException