Quick revision
The short answer
Asynchronous work loses identity because nothing carries it across the boundary, so the standard must make identity explicit data: every event and job carries an actor block (user id, tenant, on-behalf-of chain, correlation id) in its headers or payload, never the user's access token. Consumers turn that block into a SecurityContext that represents the service acting for the original actor, delegating executors carry it across threads inside a service, audit records store both the original actor and the service that acted, and consumers only trust identity fields from producers that are authorised to set them.
What to remember
In this answer, we saw that identity is lost at three boundaries: thread hand-offs, messages and scheduled jobs. The standard carries an explicit actor block with user, tenant, on-behalf-of and correlation ids in event headers instead of access tokens, sets a consumer SecurityContext that represents the service acting for that actor, propagates it across threads with delegating executors, records both the original actor and the performing service in every audit record, and only trusts identity fields from authenticated, authorised producers.
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.
Asynchronous work loses identity because nothing carries it across the boundary, so the standard must make identity explicit data: every event and job carries an actor block (user id, tenant, on-behalf-of chain, correlation id) in its headers or payload, never the user's access token. Consumers turn that block into a SecurityContext that represents the service acting for the original actor, delegating executors carry it across threads inside a service, audit records store both the original actor and the service that acted, and consumers only trust identity fields from producers that are authorised to set them.
Why this question is asked
An audit log full of 'system' is a common finding in security reviews, and it is a real problem: when a customer asks who cancelled their order, or a regulator asks who changed a limit, 'system' answers nothing. It happens because identity is only ever attached to an HTTP request, and the moment work becomes a Kafka event, a scheduled job or a task on a pool, it falls off. Interviewers ask for a standard because the fix is not one clever class; it is a convention many teams must follow the same way. In this answer, we will look at why identity is lost, define what we carry, decide why not to carry tokens, then design the producer, the consumer, the in-service propagation and the audit record, and finish with how consumers decide whom to trust.
1Why Identity Disappears
Inside one HTTP request, the user lives in SecurityContextHolder, a per-thread holder filled by the authentication filters. Three kinds of boundary drop it. A thread hand-off (a pool, @Async, CompletableFuture) moves the work to a thread that never had the value. A message (Kafka, a queue) leaves the process entirely, and the consumer runs on its own listener thread with no request behind it. A scheduled job never had a user in the first place.
When code then asks for the current user and gets nothing, a helper somewhere returns 'system' as a fallback. So the audit log is not lying on purpose; it is recording that nobody carried the information. The standard must therefore define what to carry and where, for every boundary.
2What We Carry: an Explicit Actor Block
We define one small, versioned structure that every event and job carries. It names the actor who caused the work, the tenant, the chain of services acting on the actor's behalf, and the ids that let us join records later.
Notice what is missing: the access token. A token is a credential, not a description. It expires in minutes while an event may be processed hours later or replayed next week, it grants permissions far beyond what the consumer needs, and it would be stored in the topic for its whole retention period, where anyone who can read the topic could reuse it. We carry a description of who acted and let each service use its own credentials to act.
3Producers and Consumers
On the producer side, a small library hook (a Kafka ProducerInterceptor or a wrapper around KafkaTemplate in the shared starter) reads the current SecurityContext and correlation id and writes the headers, so no team writes this by hand. Scheduled jobs set an actor of type job with a stable name such as job:invoice-nightly, and, where a person configured or triggered the job, that person as on-behalf-of. 'system' is no longer an allowed value.
On the consumer side, a hook (a Spring Kafka RecordInterceptor or a wrapper around the listener) reads the headers and builds a SecurityContext whose principal says 'orders-worker acting for u-7f3a in tenant t-42'. The authorities in that context are the consumer service's own permissions, not the user's, so the event cannot grant more than the service may do. Business code that asks for the current actor gets u-7f3a, and authorization checks that depend on ownership or tenant still work. The hook clears the context after each record.
7 messages in order. Play them, or step through them one at a time.
The actor travels as data; each service acts with its own credentials.
4Inside One Service: Across Threads
Within a service, the same identity must survive thread hand-offs. The standard requires that every executor in the service is created through the shared starter, which wraps it with DelegatingSecurityContextExecutorService and Micrometer's context propagation, and that CompletableFuture async stages always receive that executor. This way a Kafka listener that fans work out to a pool keeps the actor, just as a controller does.
Batch frameworks get the same treatment: a Spring Batch step that processes records for many users reads the actor from each record and sets it per item, rather than running the whole job as one anonymous identity.
5The Audit Record
Every audit record stores both sides of the story: the original actor and the service that performed the action. Without the second, we cannot tell whether a change was made by the user's request or by a bug in a downstream worker; without the first, we are back to 'system'.
With the correlation id, one query joins the HTTP request, every event and every audit row of the same business action, which is what an investigator actually needs.
6Trusting Identity Fields
An identity header is just a string, so the last part of the standard decides who may set it. If any service can publish to the orders topic with any actor id, then a compromised or buggy service can attribute actions to anyone. We therefore authenticate producers to Kafka (mTLS or SASL), restrict with ACLs which service identities may write each topic, and have consumers check that the record's producer is allowed to assert identity for that topic.
For high-value flows, such as payments, we go one step further and have the producer sign the actor block (for example a short JWS over the header values with the producer's key), so the consumer can verify it even if the event passed through other services. That costs a little CPU per message, so we reserve it for topics where an attribution error would matter to an auditor.
| Decision | Chosen | Rejected because |
|---|---|---|
| What to carry | Actor block in headers | Access tokens expire, over-grant and leak via the topic |
| Who the consumer acts as | Its own service identity for the actor | Acting as the user needs the user's credentials |
| Jobs without a user | Named job actor plus who triggered it | 'system' tells an auditor nothing |
| Trust in headers | Producer auth and ACLs; signing for critical topics | Trusting any producer lets anyone impersonate |
7Follow-up questions
Why not have the consumer exchange a token for the user and call downstream services as them?
Token exchange is the right tool for synchronous calls while the user's session is live. For events processed later, there is no fresh user token to exchange, and minting user tokens from an event would let any producer obtain user-level access. Acting as the service, with the actor recorded for audit and ownership checks, keeps permissions bounded.
How do we roll this out across many teams?
We put the producer and consumer hooks, the executor wrapping and the audit client into the shared starter, so most services get the standard by upgrading one dependency. Then we add a check that fails builds or alerts when audit records arrive with a missing actor, and we track the share of 'system' records per service until it reaches zero.
What if an old producer does not send the headers yet?
The consumer records the actor as unknown with the producer's identity, rather than inventing 'system', and the metric of unknown-actor records points to the producers that still need the upgrade. During the transition we accept missing headers; after a deadline, consumers of critical topics reject them.
8Conclusion
In this answer, we saw that identity is lost at three boundaries: thread hand-offs, messages and scheduled jobs. The standard carries an explicit actor block with user, tenant, on-behalf-of and correlation ids in event headers instead of access tokens, sets a consumer SecurityContext that represents the service acting for that actor, propagates it across threads with delegating executors, records both the original actor and the performing service in every audit record, and only trusts identity fields from authenticated, authorised producers.
Open with
The root cause is that identity only exists as a ThreadLocal during an HTTP request, and three boundaries drop it: thread hand-offs, messages and scheduled jobs, so a fallback writes 'system'.
Then, if they want more
My standard makes identity explicit data. Every event carries an actor block in its headers: actor id and type, tenant, the on-behalf-of chain, a correlation id and a causation id. It deliberately does not carry the user's access token, because tokens expire before events are processed, over-grant, and would sit in the topic for its retention.
Producers set the headers through a shared interceptor, and jobs use a named job actor plus whoever triggered them, never 'system'.
Consumers use a record interceptor to build a SecurityContext meaning 'this service acting for that user in that tenant', with the service's own authorities, so business code and ownership checks still see the real actor, and the context is cleared after each record.
Inside a service, every executor comes from the starter wrapped with the delegating security wrappers and context propagation, so fan-out keeps the actor. Audit records store both the original actor and the performing service, plus the correlation id, so an investigator can join request, events and changes.
Finally, trust: producers authenticate to Kafka, ACLs limit who writes each topic, consumers check the producer may assert identity, and critical topics sign the actor block. I'd measure progress by the share of records with a missing actor.
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 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 "Asynchronous flows (Kafka events, scheduled jobs, thread pools) lose the user identity...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/f8bdc5d504/