Quick revision
The short answer
I would split by what each layer can actually know. Gateways and service meshes are good at transport security, coarse authentication and workload identity, so those move to the platform; fine-grained, data-dependent authorization needs the application's knowledge of the data and stays in the services. For defence in depth, services still validate the caller's identity rather than trusting their network position, and the split is weighed against team skills, consistency across languages and the cost of framework upgrades, then documented and tested so every layer's responsibility is clear.
What to remember
In this answer, we split security responsibilities by what each layer can know. Gateways and meshes take transport security, coarse authentication and workload identity, which are uniform and easier to get right once; fine-grained, data-dependent authorization stays in the services that own the data. Services still validate identity for defence in depth instead of trusting their position in the network, organisational factors such as skills, languages and upgrade cost adjust the balance, and the split is written down and tested at every layer.
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 split by what each layer can actually know. Gateways and service meshes are good at transport security, coarse authentication and workload identity, so those move to the platform; fine-grained, data-dependent authorization needs the application's knowledge of the data and stays in the services. For defence in depth, services still validate the caller's identity rather than trusting their network position, and the split is weighed against team skills, consistency across languages and the cost of framework upgrades, then documented and tested so every layer's responsibility is clear.
Why this question is asked
Both camps in this debate are partly right. Moving security into shared infrastructure removes duplicated code and lets one team get it right; keeping it in services means each service is safe even if something in front of it is misconfigured. Interviewers ask this leadership question to see whether we can find a principled split instead of picking a side. In this answer, we will look at what gateways and meshes do well, what only the application can decide, why services must still validate identity, which organisational factors shift the balance, and how to document and test the result.
1What Gateways and Meshes Do Well
An API gateway sits at the edge and sees every external request. It is a good place to terminate TLS, validate that a request carries a valid token, reject obviously bad traffic, apply rate limits and route. A service mesh, such as Istio or Linkerd, adds a sidecar proxy or node agent next to each service, which gives every workload a cryptographic identity and encrypts service-to-service traffic with mutual TLS, where both sides present certificates.
These are exactly the jobs that are identical for every service and hard to get right repeatedly: certificate rotation, cipher configuration, token signature validation, coarse rules like 'only the orders service may call the payments service'. Doing them once in the platform is more consistent and easier to audit than 80 hand-written versions, and it works for services written in any language.
2What Only the Application Can Decide
Fine-grained authorization depends on data the gateway does not have. 'May Alice read invoice 42?' depends on whether invoice 42 belongs to Alice's company, which is in the invoice service's database. 'May this clerk approve a refund over 1,000?' depends on the refund amount in the request body and the clerk's limits. A gateway can check that Alice is logged in and has the invoices scope; only the service can check that the specific invoice is hers.
Putting these decisions in the gateway either fails, because the data is not there, or drags business logic and data lookups into shared infrastructure, where it becomes a bottleneck owned by a team that does not understand the domain. So object-level and data-dependent authorization stays in services, written with Spring Security's method security, @PreAuthorize or an AuthorizationManager, or with a policy engine the service calls.
| Responsibility | Gateway / mesh | Service |
|---|---|---|
| TLS at the edge | yes | no |
| Service-to-service mTLS | yes (mesh) | no |
| Workload identity | yes (mesh) | uses it |
| Token signature and expiry | yes, coarse check | yes, validates again |
| Rate limits, bot traffic | yes | no |
| Scope or role for an API | coarse rules | precise rules |
| May this user see this record? | cannot know | yes |
3Defence in Depth: Do Not Trust the Network Position
The risky version of moving security out is a service that trusts any request that reaches it, on the assumption that the gateway already checked. That assumption breaks in many ordinary ways: a misconfigured route that bypasses the gateway, a new internal caller, a compromised pod inside the cluster, a debugging port left open. A service that relies only on network position is unprotected whenever the network is not what we think.
So services still validate identity themselves. In practice that is cheap: the gateway passes on the user's token, and the service validates its signature and expiry with Spring Security's resource server support, which is a few lines of configuration. The mesh adds workload identity, so the service can also check which service is calling. Two independent checks mean one mistake does not open the door.
- API gatewayTLS, token valid, rate limit, routing
- Mesh sidecarmTLS, caller workload identity, coarse allow rules
- Spring Security in the servicevalidates the token again, roles and scopes
- Business codemay this user act on this record?
4 layers, from the top down. Step through them one at a time.
Each layer checks what it can know; none relies on the one above alone.
4Organisational Factors That Shift the Balance
The technical split is a starting point; the organisation adjusts it. If teams have uneven security skills, moving more into the platform protects the weaker ones, provided there is a good starter or library for what remains in services. If the company runs services in several languages, the mesh and gateway give consistency that a Spring-only solution cannot.
Upgrade cost matters too. Every security rule written in framework code must be maintained through framework upgrades in every service, as the Spring Security 6 migration showed. Fewer rules in each service and more shared configuration in a platform starter reduce that cost. On the other hand, running a mesh is itself a significant operational commitment, and it should be adopted because we need its features, not as a way to delete application code.
5Documenting and Testing the Split
The decision only holds if everyone knows it. I would write a short security responsibility document: what the gateway guarantees, what the mesh guarantees, what every service must still do (validate tokens, enforce data-level authorization), and what a service may rely on. It belongs next to the platform starter's documentation.
Each layer's responsibility is then tested. Gateway configuration has tests for routes and token rejection; mesh policies are checked in CI; each service has authorization tests that call it directly, bypassing the gateway, and prove that it still rejects missing or invalid tokens and other users' records. Those direct tests are the proof that the service is safe on its own.
6Follow-up questions
Should the gateway pass the user's token to services or a header with the user id?
The token, or a token the gateway exchanges for an internal one. A plain header such as X-User-Id can be set by anyone who reaches the service directly, so it only works if network position is trusted, which is what we want to avoid. A signed token can be validated by the service.
Where does a policy engine such as OPA fit?
It can centralise how policies are written and evaluated while the service still supplies the data, for example the invoice's owner. That keeps decisions consistent across languages without moving data-dependent authorization into the gateway. The service remains responsible for calling it with the right facts.
Is this not duplicating token validation?
Yes, deliberately, and it costs little: validating a JWT signature with cached keys takes microseconds. The duplication buys protection against a bypassed or misconfigured gateway, which is a common real failure.
7Conclusion
In this answer, we split security responsibilities by what each layer can know. Gateways and meshes take transport security, coarse authentication and workload identity, which are uniform and easier to get right once; fine-grained, data-dependent authorization stays in the services that own the data. Services still validate identity for defence in depth instead of trusting their position in the network, organisational factors such as skills, languages and upgrade cost adjust the balance, and the split is written down and tested at every layer.
Open with
I would split responsibilities by what each layer can actually know. Gateways and meshes are good at uniform jobs: TLS termination, validating that a token is present and correctly signed, rate limits, and in a mesh, mutual TLS and workload identity between services.
Then, if they want more
Those should move to the platform, because doing them once is more consistent than eighty hand-written versions and works for every language.
But fine-grained authorization, like whether this user may see this invoice or approve this refund amount, depends on data only the service has, so it stays in the service with method security or a policy engine the service calls.
I would also insist on defence in depth: services still validate the token themselves with Spring Security's resource server support, and can check the calling workload's identity, instead of trusting that anything reaching them passed the gateway. A misrouted request or a compromised pod should not get through.
Then I would adjust for the organisation: uneven skills and several languages push more into the platform, and fewer rules in services reduce framework upgrade costs. Finally, I would document the split and test each layer, including tests that call services directly, bypassing the gateway.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 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 "Some architects want to move authentication and authorization out of application code...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/0faab13cf5/