My solution is inspired by Rob Winch's answer. Though, in my scenario, Spring was saving requests that had X-Requested-With: XMLHttpRequest
set. These were the requests I had to ignore.
I created a class to be my custom RequestCache
class.
@Service("customRequestCache")
public class CustomRequestCache extends HttpSessionRequestCache { //this class (bean) is used by spring security
@Override
public void saveRequest(HttpServletRequest request, HttpServletResponse response) {
if (!"XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
//request is not ajax, we can store it
super.saveRequest(request, response);
} else {
//do nothing, add some logs if you want
}
}
}
Then, in my spring security config:
<http>
<request-cache ref="customRequestCache" />
</http>
With this custom request cache class being used, ajax requests are no longer being stored.