Quick revision
The short answer
Coarse checks that only need the token go into the SecurityFilterChain: the request must be authenticated and carry scopes such as SCOPE_orders.read for the endpoint. Tenant checks need the resource itself, so they belong in method security or, better, in the data access layer, where every query is filtered by the authenticated principal's tenant. We never trust a tenant id from the path or the body without comparing it to the token, we answer 404 rather than 403 for other tenants' resources, and we test every endpoint with users from two tenants.
What to remember
In this answer, we split authorization into coarse and fine checks. Scope and authentication rules sit in the SecurityFilterChain, because they need only the token and the URL. Tenant isolation sits next to the data, where every query is filtered by the principal's tenant, with method security for further object rules. We never trusted tenant ids from paths or bodies, answered 404 for other tenants' resources, and made two-tenant tests part of every endpoint.
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.
Coarse checks that only need the token go into the SecurityFilterChain: the request must be authenticated and carry scopes such as SCOPE_orders.read for the endpoint. Tenant checks need the resource itself, so they belong in method security or, better, in the data access layer, where every query is filtered by the authenticated principal's tenant. We never trust a tenant id from the path or the body without comparing it to the token, we answer 404 rather than 403 for other tenants' resources, and we test every endpoint with users from two tenants.
Why this question is asked
In a multi-tenant API, one bug in authorization can show one customer's data to another, which is the kind of incident that ends contracts. Interviewers ask this question to see whether we place each check where the information it needs is available, rather than trying to do everything with URL rules. In this answer, we will recap scopes and tenants, put the coarse checks in the filter chain, see why tenant checks need the data layer, handle tenant ids that arrive in the request, choose between 404 and 403, and finish with the two-tenant tests.
1A Quick Recap: Two Different Questions
An access token, usually a JWT, answers two separate questions. Scopes answer what the calling application may do: a token with orders.read may read orders, one without it may not. Spring Security's resource server turns each scope into an authority with the SCOPE_ prefix, so the scope orders.read becomes SCOPE_orders.read. A tenant claim, for example tenant_id, answers whose data the user belongs to.
The first question can be answered from the token and the URL alone. The second cannot: to know whether order 42 belongs to the caller's tenant, we must look at order 42. That difference decides where each check lives.
- Gateway or filter chaintoken valid, authenticated, scope for the endpoint
- Controllernever trusts a tenant id from path or body
- Service with method securityobject rules: owner, status, role
- Repository and databaseevery query filtered by the principal's tenant
4 layers, from the top down. Step through them one at a time.
Each layer checks what it can know. The tenant filter at the bottom is the one that cannot be forgotten per endpoint.
2Coarse Rules in the SecurityFilterChain
URL rules are fast and visible in one place, so they are the right home for checks that depend only on the path, the method and the token.
Notice the comment on the first result. Passing the filter chain only means the caller may read orders in general. It says nothing about which orders. A user of tenant A with orders.read would still reach the controller for an order of tenant B.
3Tenant Checks Next to the Data
We first make the tenant available as a small typed object taken from the token, so no code reads it from anywhere else.
Putting the tenant into the query, rather than loading the order and comparing afterwards, has two advantages. The database never returns another tenant's row, so a forgotten if statement cannot leak it, and list endpoints get the same protection as single-object endpoints. To make it impossible to forget, many teams apply the filter centrally: Hibernate 6 supports a @TenantId column filled and filtered from a CurrentTenantIdentifierResolver, and PostgreSQL row-level security can enforce it in the database itself. Method security with @PreAuthorize or @PostAuthorize remains useful for rules beyond the tenant, such as only the order's owner may cancel it.
4Never Trust a Tenant Id From the Request
Many APIs put the tenant in the URL, such as /api/tenants/acme/orders, or accept it in a request body. That is fine for routing, but the value is chosen by the caller, so it is a claim, not a fact.
The same rule applies to bodies: when a client creates an order, we set the tenant from the token and ignore or reject any tenant id it sends. Staff who legitimately work across tenants, such as support engineers, should get a separate explicit permission and an audit log entry, not a token that skips the filter.
5404 or 403 for Another Tenant's Resource?
A 403 Forbidden says the resource exists but you may not see it. For another tenant's order, that alone leaks information: an attacker can count orders or confirm that a customer id exists. So for resources owned by other tenants we answer 404 Not Found, exactly as if the id did not exist. The data-layer filter gives us this naturally, because the query simply finds nothing. We keep 403 for cases where existence is not secret, such as a user of the right tenant who lacks a scope, so client developers still get a useful error.
6Testing With Two Tenants
The most valuable test fixture in a multi-tenant system is a second tenant. For every endpoint, we create data for tenant A and call it with a token for tenant B, expecting 404 or an empty list. spring-security-test can build JWT authentications directly.
Writing this test by hand for 80 endpoints is tedious, so teams often generate it: a parameterised test reads all handler mappings and calls each with a foreign tenant's ids. The point is that cross-tenant access is tested as a property of the whole API, not remembered endpoint by endpoint.
7Follow-up questions
Why not put the tenant check in a servlet filter?
A filter can compare a tenant id in the path with the token, which is a useful first line, but it cannot know which tenant owns order 42 without loading it. The data-layer filter is the only place that covers every query, including ones whose URLs carry no tenant at all.
Is a separate database per tenant safer?
It gives stronger isolation and makes per-tenant backups and deletion easy, and some regulated customers require it. It costs more to run and migrate when there are thousands of tenants. Even then, the application must choose the connection from the token's tenant, never from the request, so the same principle applies.
Where do scopes come from in this design?
The authorization server issues them when the client asks for them and the user or administrator consents. The API decides which scope each endpoint needs and checks it in the filter chain.
8Conclusion
In this answer, we split authorization into coarse and fine checks. Scope and authentication rules sit in the SecurityFilterChain, because they need only the token and the URL. Tenant isolation sits next to the data, where every query is filtered by the principal's tenant, with method security for further object rules. We never trusted tenant ids from paths or bodies, answered 404 for other tenants' resources, and made two-tenant tests part of every endpoint.
Open with
I would put each check where its information is. The SecurityFilterChain handles what the token and URL can decide: the request must be authenticated, and each endpoint needs the right scope, for example hasAuthority SCOPE_orders.read for reads and SCOPE_orders.write for writes, with denyAll as the last rule so a forgotten endpoint fails closed.
Then, if they want more
Tenant isolation cannot be done there, because it needs to know who owns the resource. So I would take the tenant from a claim in the token, expose it through one small resolver, and filter every query by it, for example findByIdAndTenantId, or centrally with Hibernate's tenant id support or row-level security.
Method security then covers extra object rules like only the owner may cancel. If the tenant appears in a path or body, I treat it as a claim and compare it with the token's tenant, or overwrite it on create. For another tenant's resource I return 404, not 403, so we do not reveal that it exists.
And every endpoint gets a test with a user from a second tenant, which should get 404 or an empty list.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- 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 Security reference
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 "Design authorization for a multi-tenant REST API where users can only access resources...": https://scrivio-interview-library.netlify.app/spring-boot/authorization/507144b1ff/