Skip to content

Invoking RedirectView.render from within a filter throws null pointer exception [SPR-10937] #15565

@spring-projects-issues

Description

@spring-projects-issues

Ben Jordan opened SPR-10937 and commented

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".


Affects: 3.2.4

Referenced from: commits 4ac6801, 70907fd

1 votes, 4 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions