Quick revision
The short answer
I would set one rule per kind of concern. Concerns that apply to every request and need no handler information, such as request logging and tenant resolution from a header, go in servlet filters with defined order values; handler-aware concerns, such as checking an annotation on the controller method, go in interceptors; authentication and authorisation stay in Spring Security rather than custom filters; and rate limiting usually belongs at the gateway. The shared filters and interceptors ship in a platform starter with documented order values, so every service gets the same behaviour.
What to remember
In this answer, we set one rule per concern: filters with fixed order values for request-wide work such as correlation ids, logging and tenant resolution; interceptors for handler-aware checks; Spring Security for authentication and authorisation; the gateway for rate limiting, with a shared filter only for business quotas; and AOP for service-level concerns. We published order constants relative to Security's -100 and shipped everything in a tested starter so that following the standard is the easiest path.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would set one rule per kind of concern. Concerns that apply to every request and need no handler information, such as request logging and tenant resolution from a header, go in servlet filters with defined order values; handler-aware concerns, such as checking an annotation on the controller method, go in interceptors; authentication and authorisation stay in Spring Security rather than custom filters; and rate limiting usually belongs at the gateway. The shared filters and interceptors ship in a platform starter with documented order values, so every service gets the same behaviour.
Why this question is asked
When each service invents its own place for authentication, tenant resolution, rate limiting and logging, the result is not just untidy. Bugs hide in the gaps: a tenant check that runs after logging, so logs lack the tenant; a custom auth filter that misses async dispatches; an AOP aspect that never sees requests rejected earlier. Interviewers ask this at a senior level to see whether we can turn knowledge of the request pipeline into a standard that many teams can follow. In this answer, we will recap the three mechanisms, set a rule for each concern with the reason behind it, define the order, and package the result so that following the standard is the easy path.
1A Quick Recap of the Three Mechanisms
A servlet filter runs in the container's filter chain before DispatcherServlet. It sees every request, including static files and error dispatches, and can wrap the request and response, but it knows nothing about controllers. A HandlerInterceptor runs inside DispatcherServlet once the handler is chosen, so it can read the controller method and its annotations, but it only sees requests that map to a handler and cannot replace the request object.
AOP, aspect-oriented programming, wraps Spring bean method calls through proxies. It works at the level of service methods, not HTTP requests, so it does not know about headers or status codes unless we dig them out of a ThreadLocal. It is the right tool for method-level concerns like transactions or caching, and usually the wrong tool for web concerns.
2Placing Each Concern and Why
Request logging and correlation ids apply to every request and do not depend on the handler, so they are filters. They must run early so that everything after them, including Spring Security, logs with the same correlation id.
Tenant resolution depends on where the tenant comes from. If it is read from a header or the host name and stored for the rest of the request, it is a filter, placed after Security when it must be checked against the authenticated user. If it only feeds controller parameters, an argument resolver is cleaner. Either way, there is one shared implementation.
Authentication and authorisation stay in Spring Security. Custom security filters tend to miss details that Security already handles: async and error dispatches, session fixation, the right 401 and 403 responses, and method security. If a service needs a new kind of credential, we write a Security AuthenticationProvider or use its resource-server support, rather than a filter of our own.
Handler-aware checks, such as a @RequiresFeature or @AuditedEndpoint annotation, are interceptors, because only an interceptor knows which method will run. AOP remains for service-level concerns, not HTTP ones.
| Concern | Mechanism | Reason |
|---|---|---|
| Correlation id, request logging | Filter, very early | every request, logs from Security included |
| Authentication, authorisation | Spring Security | handles dispatches, sessions, 401/403 |
| Tenant from header or host | Filter after Security | checked against the user, set for the request |
| Annotation checks on handlers | Interceptor | needs the HandlerMethod |
| Rate limiting | Gateway; filter if per user | protect before traffic reaches services |
| Transactions, caching | AOP on services | method level, not HTTP |
3Rate Limiting Usually Belongs at the Gateway
Rate limiting protects capacity. If each service counts requests in its own memory, limits are per instance: with 6 instances and a limit of 100 per minute each, a client actually gets 600 per minute, and the count resets whenever an instance restarts. Requests that will be rejected still consume a connection, a thread and a trip through Security before the service says no.
A gateway or API management layer sees all traffic in one place, can share counters, and rejects excess traffic before it reaches any service. So the standard says: global and per-client limits live at the gateway. A service-level limiter, backed by a shared store such as Redis, is allowed only for limits that need business knowledge the gateway lacks, such as a per-account quota on an expensive export endpoint, and then it is a shared filter placed after Security so it knows the user.
4Defining the Order
Most of the subtle bugs come from order, so the standard publishes fixed order values relative to Spring Security's chain, which Spring Boot registers at spring.security.filter.order, -100 by default. Lower numbers run first.
- Gatewayrate limits, TLS
- Correlation and logging filters-300 and -250
- Spring Security-100: authentication and authorisation
- Tenant and quota filters-50 and -40
- DispatcherServlet and interceptorsannotation checks on handlers
- Controllers and servicesAOP for transactions and caching
6 layers, from the top down. Step through them one at a time.
Teams that add their own filter pick a value relative to these constants, not a random number, and the gaps leave room for later additions. Interceptors are ordered too, with InterceptorRegistration.order(), and the starter registers its interceptors with known values for the same reason.
5Shipping It in a Starter
A standard written only on a wiki drifts within months. So the filters, interceptors and order constants ship in a platform starter that registers them through auto-configuration. A service gets correlation ids, logging and tenant resolution by adding one dependency, with no code. Each component can be turned off or replaced with a property or by declaring its own bean, which keeps legitimate exceptions visible instead of hidden in custom code.
The starter has tests that pin the order: an unauthenticated request must still produce a log line with a correlation id, and a request with a tenant the user does not belong to must be rejected before any controller runs. We then migrate services gradually, starting with those that have had incidents, removing their own versions as they adopt the starter.
6Follow-up questions
Why not use AOP around controllers for logging?
Because an aspect on controller methods only sees requests that reach a controller. It misses requests rejected by Security, 404s, static files and errors, which are often the ones we most need to log. It also does not see the final status code, which is decided later. A filter sees all of that.
What if a team really needs a custom security filter?
We first look for a Spring Security extension point, such as an AuthenticationProvider, an AuthenticationConverter or resource-server configuration. If a filter is still needed, it goes into the SecurityFilterChain with addFilterBefore or addFilterAfter, so it runs inside Security's chain with its error handling, and it gets reviewed by the security owners.
How do you know services actually follow the standard?
The starter reports its version and enabled components through an info endpoint or a startup log line, and an ArchUnit rule in the service template flags classes that implement Filter or HandlerInterceptor outside the approved packages. Together they give a simple dashboard of which services are on the standard.
7Conclusion
In this answer, we set one rule per concern: filters with fixed order values for request-wide work such as correlation ids, logging and tenant resolution; interceptors for handler-aware checks; Spring Security for authentication and authorisation; the gateway for rate limiting, with a shared filter only for business quotas; and AOP for service-level concerns. We published order constants relative to Security's -100 and shipped everything in a tested starter so that following the standard is the easiest path.
Open with
I'd base the standard on what each mechanism can see. Filters run before DispatcherServlet and see every request, so anything that applies to all requests and doesn't need the handler goes there: correlation ids and request logging, very early, so even Spring Security's log lines carry the id.
Then, if they want more
Tenant resolution from a header is also a filter, but after Security, so we can check the tenant against the authenticated user. Interceptors run once the handler is known, so they're for handler-aware rules like checking an annotation on the controller method.
Authentication and authorisation stay in Spring Security, no home-made auth filters, because Security already handles async and error dispatches, sessions and correct 401s and 403s. If something custom is needed it goes inside the SecurityFilterChain.
Rate limiting I'd push to the gateway, because per-instance counters multiply: six instances with 100 a minute each means 600 a minute in practice, and rejected traffic still costs threads. A service-level limiter is only for business quotas, backed by a shared store. AOP stays for service-level things like transactions.
Then the important part is packaging: a platform starter that auto-configures these filters and interceptors with published order constants relative to Security's minus 100, properties to opt out, and tests that pin the order. And we migrate gradually, starting with services that had incidents.
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
- Spring Framework reference, Spring Web MVC
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 "Cross-cutting concerns such as authentication, tenant resolution, rate limiting and...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/1945bd5598/