Skip to content

Conversation

@sic2
Copy link
Contributor

@sic2 sic2 commented Oct 12, 2025

The introduction of shouldFilter allows for intentional filtering.

Usage example:

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");
    }
}

Closes gh-35605

sic2 added 2 commits October 12, 2025 10:30
The introduction of shouldFilter allows for intentional filtering.

Usage example:
```java
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");
    }
}
```

Closes spring-projectsgh-35605

Signed-off-by: Simone Conte <[email protected]>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 12, 2025
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Oct 12, 2025
@sbrannen sbrannen changed the title Introduce shouldFilter in OncePerRequestFilter Introduce shouldFilter flag in OncePerRequestFilter Oct 12, 2025
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Oct 13, 2025

Filters are already mapped to a specific URL or set of URLs, and it makes sense to narrow that down with an exclude list. So in the example you show, the Filter could be mapped to "/booking/**", and it would only be invoked for that subpath.

Could you expand on what you're trying to achieve, including how the Filter needs to be mapped?

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Oct 13, 2025
@sic2
Copy link
Contributor Author

sic2 commented Oct 13, 2025

Filters are already mapped to a specific URL or set of URLs, and it makes sense to narrow that down with an exclude list. So in the example you show, the Filter could be mapped to "/booking/**" and it would only be invoked for that subpath.

Could you expand on what you're trying to achieve, including how the Filter needs to be mapped?

@rstoyanchev I take it you are referring to the use of AbstractFilterRegistrationBean (and implementations) from spring-boot for the mapping of URLs via setUrlPatterns?
Looking at it, I see what you mean. This is a miss from me.

In my mind, shouldNotFilter function in the OncePerRequestFilter is missing a counter part at the same level of abstraction, but I could be interpreting this all wrong.

Examples

The way I see it, we would still be mapping the filter using FilterRegistrationBean or (similar), but this would be complemented by logic from the filters themselves.

Via URI (as in tests)

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

Via other bits of request (e.g. method)

@Override
protected boolean shouldFilter(HttpServletRequest request) {
	return "POST".equals(request.getMethod());
}

Moving forward

However, I get that there is already a way to perform filtering, even if it is by URL patterns only.

Feel free to close this PR if you think this doesn't fit your architectural design.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 13, 2025
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Oct 13, 2025

Thanks for the extra comments. For mapping Filter's, I was referring to the Servlet API, see for example FilterRegistration, but yes in Boot this is exposed as a FilterRegistrationBean.

I will close this for now since OncePerRequestFilter has quite a few options already, and the idea generally is, map the Filter in the Servlet container, and then narrow via exclusions. This should work well even for HTTP methods as there are very few, so you can filter them either as exclusions or as inclusions.

We can always reconsider for concrete uses cases.

@rstoyanchev rstoyanchev added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Oct 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce shouldFilter in OncePerRequestFilter

4 participants