Quick revision
The short answer
We register the partner under spring.security.oauth2.client.registration with the client id, secret, client_credentials grant and scope, plus the provider's token-uri. An OAuth2AuthorizedClientManager obtains the token, caches it and fetches a new one when it expires; outside a web request we use AuthorizedClientServiceOAuth2AuthorizedClientManager. Since Spring Security 6.4, OAuth2ClientHttpRequestInterceptor attaches the token to every RestClient request, and for WebClient the OAuth2 exchange filter function does the same.
What to remember
In this answer, we called a partner API with the client credentials grant. We registered the partner with client id, secret, grant type, scope and token-uri, kept the secret in a secret store, and built an AuthorizedClientServiceOAuth2AuthorizedClientManager so tokens work outside web requests. OAuth2ClientHttpRequestInterceptor, available since Spring Security 6.4, attaches the token to RestClient calls, the exchange filter function does it for WebClient, and the manager reuses the cached token and fetches a new one shortly before expiry.
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.
We register the partner under spring.security.oauth2.client.registration with the client id, secret, client_credentials grant and scope, plus the provider's token-uri. An OAuth2AuthorizedClientManager obtains the token, caches it and fetches a new one when it expires; outside a web request we use AuthorizedClientServiceOAuth2AuthorizedClientManager. Since Spring Security 6.4, OAuth2ClientHttpRequestInterceptor attaches the token to every RestClient request, and for WebClient the OAuth2 exchange filter function does the same.
Why this question is asked
Service-to-service calls with OAuth2 are common, and the first version many teams write fetches a token by hand before every call, or caches it in a static field and forgets about expiry. Both break in production: one hammers the partner's token endpoint, the other fails every request once the token expires. Interviewers ask this to see whether we know the pieces Spring Security already provides. In this answer, we will recap the client credentials grant, write the registration, build the authorized client manager, wire it into RestClient, and look at how tokens are refreshed and secrets are kept safe.
1A Quick Recap of the Client Credentials Grant
In the client credentials grant there is no user. Our service is the client, and it proves its own identity to the partner's token endpoint with a client id and secret. The token endpoint returns an access token, usually valid for a few minutes to an hour, which we then send as a bearer token to the partner API.
Notice what that means for refreshing. This grant normally returns no refresh token, so "refreshing" simply means asking for a new access token with the same credentials when the old one is about to expire. Spring does that for us if we let it own the token.
2Registering the Partner
The registration describes how to get a token; the provider block says where the token endpoint is. The registration id, here partner, is the name we use in code.
The secret is a reference to an environment variable that a secret store such as Vault or a cloud secret manager provides at runtime. Writing the real value into application.yml would put it into the Git history, where it stays even after we remove it.
3The Authorized Client Manager
An OAuth2AuthorizedClientManager is the component that answers "give me a valid token for registration partner". It looks in its store for a token, returns it if it is still valid, and otherwise asks the token endpoint for a new one and stores it.
Spring offers two implementations. DefaultOAuth2AuthorizedClientManager works inside an HTTP request and stores tokens per request and session, which suits a user logged in to a web app. Our partner calls may also run from a scheduled job or a Kafka listener, where there is no request. For those we use AuthorizedClientServiceOAuth2AuthorizedClientManager, which stores tokens in an OAuth2AuthorizedClientService and works anywhere.
4Attaching the Token to RestClient
Since Spring Security 6.4, OAuth2ClientHttpRequestInterceptor plugs the manager into RestClient. Before each request, it asks the manager for a token for the chosen registration and adds the Authorization header.
11 messages in order. Play them, or step through them one at a time.
If every call of this client goes to the same partner, we can give the interceptor a default registration id instead of setting the attribute each time. For WebClient, the equivalent is ServletOAuth2AuthorizedClientExchangeFilterFunction in a servlet application, or ServerOAuth2AuthorizedClientExchangeFilterFunction in a reactive one.
5How Expiry Is Handled
The client credentials provider treats a token as expired a little before its real expiry, 60 seconds by default, so a token is not sent just as it runs out. When that moment comes, the next call quietly runs the grant again. If the partner rejects a token early, for example after they revoke it, the interceptor's failure handling can remove the stored token on a 401 so the next call fetches a fresh one; check your version for the exact default behaviour.
The default OAuth2AuthorizedClientService keeps tokens in memory, which is fine for one access token per registration. With many instances, each one fetches its own token, which is normally acceptable; a JDBC-backed service exists if we need tokens shared or kept across restarts.
6Follow-up questions
Why not call the token endpoint ourselves and store the token in a field?
Because we would have to reimplement expiry, clock skew, thread safety and error handling, and those are the parts that fail in production. The manager already does them, and the interceptor keeps token handling out of business code.
What changes if the partner wants private_key_jwt instead of a client secret?
We set client-authentication-method to private_key_jwt and provide a function that supplies our signing key to the token request, through NimbusJwtClientAuthenticationParametersConverter. No secret is then sent at all, which many partners prefer. Check your version for the exact configuration.
Is there something newer than the interceptor?
In Spring Security 7 with Spring Framework 7, declarative HTTP service clients can carry a @ClientRegistrationId annotation so the token is attached without setting attributes. The interceptor and manager underneath are the same. Check your version.
7Conclusion
In this answer, we called a partner API with the client credentials grant. We registered the partner with client id, secret, grant type, scope and token-uri, kept the secret in a secret store, and built an AuthorizedClientServiceOAuth2AuthorizedClientManager so tokens work outside web requests. OAuth2ClientHttpRequestInterceptor, available since Spring Security 6.4, attaches the token to RestClient calls, the exchange filter function does it for WebClient, and the manager reuses the cached token and fetches a new one shortly before expiry.
Open with
I would let Spring Security own the token. In application.yml I register the partner under spring.security.oauth2.client.registration.partner with the client id, authorization-grant-type client_credentials and the scope, and a provider entry with the partner's token-uri.
Then, if they want more
The client secret is not in the file; it comes from a secret store through an environment variable. Then I define an OAuth2AuthorizedClientManager. Because these calls may run from scheduled jobs or message listeners, I use AuthorizedClientServiceOAuth2AuthorizedClientManager with a provider built for client credentials, rather than the default manager that needs an HTTP request.
With Spring Security 6.4 or later, I create the RestClient with an OAuth2ClientHttpRequestInterceptor built on that manager, and choose the registration with the clientRegistrationId request attribute or a default resolver.
On the first call the manager runs the grant and caches the token; later calls reuse it, and about a minute before it expires the next call fetches a new one, since client credentials normally has no refresh token. For WebClient I would use the OAuth2 exchange filter function instead.
That keeps token handling out of business code and avoids hammering the partner's token endpoint.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 6, Spring Framework 7, Spring Security 7
- 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
- RFC 6749, The OAuth 2.0 Authorization Framework, Spring Security reference, OAuth2
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 "A Spring Boot service needs to call a partner API using the client credentials grant....": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/a080485aab/