Skip to content

Introduce shouldFilter in OncePerRequestFilter #35605

@sic2

Description

@sic2

Hi, I was thinking whether it makes sense to add a boolean shouldFilter(HttpServletRequest request) method in OncePerRequestFilter.

Why

Today, shouldNotFilter can be used to block filtering on specific requests.
The introduction of shouldFilter would enable for more intentional filtering.

public class BookingsFilter extends OncePerRequestFilter {
    
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
				FilterChain filterChain) { ... }

    @Override
    protected boolean shouldFilter(HttpServletRequest request) throws ServletException {
        return request.getRequestURI().startsWith("/booking");
    }
}

Pseudo-implementation

protected boolean shouldFilter(HttpServletRequest request) throws ServletException {
	return true;
}

In the situation that a request is satisfied by both shouldNotFilter and shouldFilter, the shouldNotFilter will take precedence. Changes in the doFilter method would be:

- else {
+ else if (shouldFilter(httpRequest)) {
	 // Do invoke this filter...
	 request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);

Contributing

I am happy to take this on, provided the idea is accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions