gRPC Interceptors for Auth and Logging
Fast recall
Quick revision
Core answer
ServerInterceptor (or ClientInterceptor) and register it with @GrpcGlobalServerInterceptor or per-service via @GrpcService(interceptors = ...). Inside the interceptor, access the Metadata (headers) for tokens, call Context for propagating values, and delegate to the next handler or abort with a Status.UNAUTHENTICATED error.Tests cross-cutting concern design in gRPC — equivalent to Servlet filters or Spring AOP.
Interviewers want to see you can centralise auth and observability without cluttering service logic.
Interviewers want to see you can centralise auth and observability without cluttering service logic.
Interview-ready guide
Interview answer
Interceptors are gRPC's equivalent of servlet filters — they wrap every RPC call so you can centralize logging, authentication, and metrics without touching service methods. Implement ServerInterceptor, annotate with @GrpcGlobalServerInterceptor, and the starter applies it to every service automatically.
Logging interceptor:
- Wrap the
ServerCallwithForwardingServerCallto capture the method name at entry and the status code at exit; override only the methods you need — typicallyclose()for response logging — and delegate everything else to avoid breaking flow control or cancellation propagation - This gives structured request-response logs without modifying any service code
Auth interceptor:
- Read the
Authorizationheader from gRPC'sMetadata, validate the JWT, and either close the call withStatus.UNAUTHENTICATEDor attach the principal to aContext.KeyusingContexts.interceptCall() - Return a no-op
new ServerCall.Listener<>() {}after callingcall.close()to prevent the service method from executing on rejected calls — without this, the service still runs after rejection
The context propagation model — the most important detail:
- Unlike Spring Security's
SecurityContextHolderwhich usesThreadLocal, gRPC'sContextis explicitly propagated throughContexts.interceptCall() - If you use
ThreadLocalfor auth state, it works fine for unary calls but silently breaks on streaming RPCs where message handlers run on different threads —Context.Keypropagates correctly across all gRPC execution models
I recommend always using Context.Key for anything request-scoped in gRPC, and setting @Order on interceptors so auth runs before logging — rejected calls should still appear in access logs.
gRPC Interceptors for Auth and Logging
Direct Answer
Implement ServerInterceptor (or ClientInterceptor) and register it with @GrpcGlobalServerInterceptor or per-service via @GrpcService(interceptors = ...). Inside the interceptor, access the Metadata (headers) for tokens, call Context for propagating values, and delegate to the next handler or abort with a Status.UNAUTHENTICATED error.
Common Mistakes & Pitfalls
Interviewers want to see you can centralise auth and observability without cluttering service logic.
Check your understanding
Answer these without looking back at the article.
- How do you apply an interceptor to only one specific gRPC service?
- How do you integrate gRPC interceptors with Spring Security's SecurityContextHolder?
- How do you propagate OpenTelemetry trace context through gRPC interceptors?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.