Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

A Spring Boot application has a custom servlet filter that logs requests, Spring Security's filter chain, and a HandlerInterceptor that checks a tenant header. In what order do they run for one request, and why does a request rejected by Spring Security never reach the interceptor?

The servlet container runs the registered filters in order first, and Spring Security is one of those filters, a DelegatingFilterProxy that hands the request to FilterChainProxy and its own internal chain of security filters. Only when every filter has passed the request on does DispatcherServlet run, and only then do HandlerInterceptors run around the chosen controller. A security filter that rejects the request writes the 401 or 403 response itself and never calls the rest of the chain, so DispatcherServlet and the interceptor never see it; logging that must include rejected requests belongs in a filter ordered before Spring Security.

Background to this question

A common surprise in Spring Boot applications is that some requests never appear in a log or never hit a tenant check, and it turns out they were rejected before reaching that code. Understanding the order in which filters, the security chain and interceptors run explains it immediately, and it tells us where each kind of cross-cutting logic belongs. In this answer, we will recap filters and interceptors, follow one request through all three components, see what happens when Spring Security rejects it, and set the order so the logging filter sees everything.

1A Quick Recap of Filters and Interceptors

A servlet filter is a component of the Servlet API that wraps request processing. Its doFilter(request, response, chain) method can do something before, call chain.doFilter(request, response) to pass the request on, and do something after. If it does not call chain.doFilter, the request stops there. Filters belong to the servlet container and run before any servlet is called.

A HandlerInterceptor is a Spring MVC component. It has preHandle, postHandle and afterCompletion methods, and it runs inside DispatcherServlet, around the controller method that was selected for the request. So an interceptor knows which handler will run, while a filter only knows the raw request.

2The Order for One Request

For a request that is allowed, the order is: the container's filter chain in order of each filter's position, then DispatcherServlet, then the interceptor's preHandle, the controller, postHandle and afterCompletion. Spring Security takes part as one entry in the container's filter list. Spring Boot registers a DelegatingFilterProxy named springSecurityFilterChain, which delegates to FilterChainProxy, and FilterChainProxy runs its own internal list of security filters: loading the security context, authentication filters, exception translation and authorization.

A filter's position depends on its order. Spring Boot registers the security filter at order -100 by default (the property spring.security.filter.order). A @Component filter with no order gets the lowest precedence, so it runs after security. A filter with @Order(Ordered.HIGHEST_PRECEDENCE), or any value below -100, runs before it.

Step throughOne request through filters, security and an interceptor
  1. Logging filterorder HIGHEST_PRECEDENCE: always runs
  2. Spring Security filterorder -100: DelegatingFilterProxy -> FilterChainProxy
  3. Authenticated and authorized?
    Yeschain.doFilter: continueNoWrite 401 or 403 and stop
  4. DispatcherServletfinds the handler for the URL
  5. Tenant interceptor preHandlechecks the X-Tenant header
  6. Controller methodthen postHandle and afterCompletion

6 steps in order. Play them, or step through them one at a time.

A rejected request leaves at the decision step and never reaches the bottom half.

3Seeing It in Code

Here are the three components. The logging filter is ordered first; the interceptor is registered through WebMvcConfigurer.

Java
@Component
@Order(Ordered.HIGHEST_PRECEDENCE) // runs before Spring Security (-100)
class RequestLogFilter extends OncePerRequestFilter {
@Override protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
throws ServletException, IOException {
try { chain.doFilter(req, res); }
finally { log.info("{} {} -> {}", req.getMethod(), req.getRequestURI(), res.getStatus()); }
}
}
class TenantInterceptor implements HandlerInterceptor {
@Override public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) {
log.info("tenant check for {}", handler);
return req.getHeader("X-Tenant") != null; // false stops before the controller
}
}
@Configuration
class WebConfig implements WebMvcConfigurer {
@Override public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TenantInterceptor()).addPathPatterns("/api/**");
}
}
// GET /api/orders with a valid token: tenant check for OrderController#list ; GET /api/orders -> 200
// GET /api/orders with no token: GET /api/orders -> 401 (no tenant check line)

Notice the two log outputs. With a valid token, both the interceptor and the filter log. Without a token, only the filter logs, with status 401. The interceptor line never appears, because the request never reached DispatcherServlet.

4Why a Rejected Request Never Reaches the Interceptor

Inside the security chain, AuthorizationFilter decides whether the request is allowed. When it is not, it throws an access denied exception, which ExceptionTranslationFilter catches. For an unauthenticated user, the configured entry point writes the response, a 401 for an API using HTTP Basic or bearer tokens, or a redirect to the login page for form login. For an authenticated user without permission, the access denied handler writes a 403.

In both cases the security filter has written the response and returns without calling chain.doFilter. The container's filter chain simply unwinds: the filters before security get control back in their after part, which is why our logging filter can see the 401, and nothing after security runs. DispatcherServlet's service() is never called, so it never looks up a handler, and an interceptor attached to a handler has nothing to attach to.

One nuance: when the entry point uses response.sendError, the container may then perform an error dispatch to Spring Boot's /error path, which goes through DispatcherServlet to the error controller. Any interceptor mapped to that path could run for that separate dispatch, but never for the original controller. Check how your Boot version and security configuration handle the error dispatch.

5Putting Each Concern in the Right Place

So the placement follows from the order. Logging, metrics or request ids that must cover every request, including rejected ones, go in a servlet filter ordered before Spring Security. Logic that needs the authenticated user goes either inside the security chain, added with addFilterAfter, or in an interceptor or controller code, which run after authentication. Logic that needs to know the handler, such as a tenant check that varies by controller or annotation, fits an interceptor. And anything that security should protect must not run before security.

6Follow-up questions

How do you set the order of a custom filter in Spring Boot?

There are three ways. If the filter is a @Component, put @Order on the class or implement Ordered and return the order from getOrder(); lower values run first. If we need more control, we declare a FilterRegistrationBean for the filter, call setOrder, and can also set URL patterns and dispatcher types; in that case the filter should not also be picked up as a separate component registration. To place a filter inside Spring Security's own chain instead, relative to specific security filters, we use http.addFilterBefore or addFilterAfter in the SecurityFilterChain configuration. Spring Security's servlet filter itself sits at -100 by default, configurable with spring.security.filter.order.

Does postHandle run if the controller throws?

No. postHandle runs only after the handler returns normally. afterCompletion runs in both cases, with the exception passed in, so clean-up belongs there.

Can an interceptor reject a request like a filter can?

Yes, by returning false from preHandle after writing a response, for example a 400 for a missing tenant header. But it only runs for requests that reached DispatcherServlet and matched a handler.

7Conclusion

In this answer, we saw that the container runs filters in order first, that Spring Security is a single filter, DelegatingFilterProxy to FilterChainProxy, at order -100 by default with its own internal chain, and that DispatcherServlet and then HandlerInterceptors run only after every filter has passed the request on. A security filter that rejects a request writes the response and does not call the rest of the chain, so the interceptor never runs, and logging that must see rejected requests belongs in a filter ordered before Spring Security.

What to say in the interviewAbout 100 seconds aloud

Open with

For one request, the servlet container runs the filter chain first, in order. Spring Security is one of those filters: Boot registers a DelegatingFilterProxy that hands the request to FilterChainProxy, which runs its own internal chain of security filters.

Then, if they want more

Its position is order minus 100 by default. A custom filter's position depends on its order: a @Component without @Order runs after security, and one with @Order(Ordered.HIGHEST_PRECEDENCE) runs before it.

Only when every filter has called chain.doFilter does the container call DispatcherServlet, which picks the handler, and only then does the HandlerInterceptor's preHandle run, then the controller, then postHandle and afterCompletion. So for my three components, the logging filter runs first if I ordered it before security, then the security chain, then DispatcherServlet with the tenant interceptor around the controller.

When Spring Security rejects a request, for example no token, AuthorizationFilter throws, ExceptionTranslationFilter catches it, and the entry point writes a 401, or the access denied handler writes a 403. That filter returns without calling chain.doFilter, so the chain unwinds back through the earlier filters and DispatcherServlet is never called.

The interceptor is attached to a handler, and no handler was ever chosen, so it cannot run. That is why logging that must see rejected requests, like access logs or request ids, goes in a filter ordered before Spring Security, while logic that needs the user goes inside the security chain with addFilterAfter or in an interceptor. To set filter order, I use @Order, Ordered, or FilterRegistrationBean.setOrder.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Jakarta EE specifications
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Spring Boot application has a custom servlet filter that logs requests, Spring...": https://scrivio-interview-library.netlify.app/java/servlets/24944c47df/