Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Invoking RedirectView.render from within a filter throws null pointer exception [SPR-10937] #15565
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:
In RedirectView's renderMergedOutputModel method, a null pointer exception (silently for some reason) occurs because the following line returns null:
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".
1 votes, 4 watchers
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?
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:
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.