Affects: Spring Framework version 5.3.27


Current org.springframework.web.filter.OncePerRequestFilter class has two methods shouldNotFilterAsyncDispatch() and shouldNotFilterErrorDispatch() which are supposed to be overridden by its inherited classes if necessary. However, there are many classes which inherit from this one and don't override the method. Moreover, some of them are also marked as final which blocks the possibility to override this filter, e.g. CsrfFilter.

I suggest an enhancement for this call following the way we applied for skipDispatch(HttpServletRequest) so that in case some filters don't override this method we can let it work as expected like this

@Override
protected boolean shouldNotFilterAsyncDispatch(HttpServletRequest request) throws ServletException {
    return Boolean.TRUE.equals(request.getAttribute("SHOULD_NOT_FILTER_ASYNC_REQUEST"));
}

and the same for shouldNotFilterErrorDispatch:

@Override
protected boolean shouldNotFilterErrorDispatch(HttpServletRequest request) throws ServletException {
    return Boolean.TRUE.equals(request.getAttribute("SHOULD_NOT_FILTER_ASYNC_ERROR_REQUEST"));
}

The code above is inspired by this tutor: https://www.baeldung.com/spring-onceperrequestfilter

I think this will be much more flexible for people who have legacy code from the old time which utilize the ASYNC request in the old way.

Comment From: sbrannen

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

Comment From: rstoyanchev

However, there are many classes which inherit from this one and don't override the method

Not all filters need to handle the ASYNC dispatch, in which case there is no need to override the method.

Moreover, some of them are also marked as final which blocks the possibility to override this filter, e.g. CsrfFilter.

That is a design choice for each filter and they are marked as such that means they don't want to allow flexibility there.

I don't think adding yet another method to decide whether to filter or not is going to make it easier to understand how the class works or what it does.