It seems that when CSRF protection is enabled, Spring Security only puts your original request in the requestCache
if the request used the GET
method. In order to have it cache POST
requests as well, I created a custom requestCache
.
I'm not 100% convinced that doing so doesn't weaken the CSRF protection somehow, but it seems safe in my mind.
Add request cache bean to the XML configuration:
<bean id="requestCache" class="a.b.c.AlwaysSaveRequestCache" />
<http>
<csrf />
<request-cache ref="requestCache" />
</http>
Implement the custom request cache, by extending and borrowing code from HttpSessionRequestCache
:
public class AlwaysSaveRequestCache extends HttpSessionRequestCache
{
@Override
public void saveRequest(HttpServletRequest request, HttpServletResponse response)
{
final String SAVED_REQUEST = "SPRING_SECURITY_SAVED_REQUEST";
DefaultSavedRequest savedRequest = new DefaultSavedRequest(request, new PortResolverImpl());
request.getSession().setAttribute(SAVED_REQUEST, savedRequest);
logger.debug("DefaultSavedRequest added to Session: " + savedRequest);
}
}
Your POST
requests should now be cached and re-sent after being interrupted by the login form.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…