Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-issuemaster opened this issue Sep 25, 2013 · 3 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Sep 25, 2013

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 2, 2013

Rossen Stoyanchev commented

I guess I'm wondering do you need to use RedirectView, which is normally used in a very different context (i.e. invoked by the DispatcherServlet), as opposed to performing the redirect yourself through the HttpServletResponse?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Nov 4, 2013

Anderson Freitas commented

A workaround that solved the problem for me is to set the FLASH_MAP_MANAGER_ATTRIBUTE into your request just before you invoke the redirect:
request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
redirect.render(model, request, response);

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Nov 5, 2013

Juergen Hoeller commented

It seems easy enough to move the FlashMapManager call to within the if block, that is, to only retrieve the FlashMapManager if a non-empty output FlashMap has been found. This doesn't seem to hurt and keeps RedirectView usable outside of a DispatcherServlet request, so I've applied it for 4.0 RC2 as well as 3.2.5.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.