Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-10937

Invoking RedirectView.render from within a filter throws null pointer exception

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 3.2.4
    • Fix Version/s: 3.2.5, 4.0 RC2
    • Component/s: Web
    • Labels:
    • Last commented by a User:
      true

      Description

      Before I get into this, I'm willing to accept that what I'm doing is wrong (very old code). If so feel free to close this.

      I have a OncePerRequestFilter that is invoked in the Spring Security chain after the FORM_LOGIN_FILTER filter. Inside this filter, the code decides whether some conditions are true and if so it redirects the user to a new page:

      public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
          if (someEvent) {
              new RedirectView(someUrl, true).render(model, request, response);
              return; // Stop the filter chain
          }
          chain.doFilter(request, response); // Keep going
      }
      

      In RedirectView's renderMergedOutputModel method, a null pointer exception (silently for some reason) occurs because the following line returns null:

      FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
      flashMapManager.saveOutputFlashMap(flashMap, request, response); // Null Pointer
      

      Having thrown some breakpoints around it seems to be because the DispatcherServlet hasn't added the FlashMapManager into the request as the filter was invoked before the DispatcherServlet.

      I can get around this by overriding renderMergedOutputModel and taking out the stuff about flash attributes as I do not need them.

      Again, happy to be told I'm "doing it wrong".

        Attachments

          Activity

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              jordanb Ben Jordan
              Last updater:
              Spring Issuemaster
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                47 weeks, 5 days ago