Quick revision
The short answer
A server-rendered web app uses the authorization code flow as a confidential client, with PKCE as current best practice. A single-page app uses authorization code with PKCE as a public client, or better, a backend-for-frontend that keeps tokens on the server. A nightly batch job uses client credentials because no user is involved, and a smart TV uses the device authorization grant, where the user approves on a phone or laptop. The implicit and resource owner password grants should no longer be used; current best practice deprecates them and the OAuth 2.1 draft omits them.
What to remember
In this answer, we chose a grant by asking whether a user is present and whether the client can keep a secret. The server-rendered app uses the authorization code flow as a confidential client with PKCE, the SPA uses code with PKCE or preferably a backend-for-frontend, the batch job uses client credentials, and the smart TV uses the device authorization grant. The implicit and password grants are deprecated by current best practice and left out of the OAuth 2.1 draft.
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.
A server-rendered web app uses the authorization code flow as a confidential client, with PKCE as current best practice. A single-page app uses authorization code with PKCE as a public client, or better, a backend-for-frontend that keeps tokens on the server. A nightly batch job uses client credentials because no user is involved, and a smart TV uses the device authorization grant, where the user approves on a phone or laptop. The implicit and resource owner password grants should no longer be used; current best practice deprecates them and the OAuth 2.1 draft omits them.
Why this question is asked
Choosing a grant, the way a client obtains a token, is one of the first decisions in any OAuth2 integration, and a wrong choice either leaks tokens or makes the product unusable. Interviewers ask this question because each case tests a different idea: whether the client can keep a secret, whether a user is present, and whether the device has a browser. In this answer, we will recap grants and client types, go through the four cases one by one with the reason for each choice, see the two grants that are now retired, and look at Spring configuration for the batch job.
1A Quick Recap: Grants and Client Types
A grant is the procedure a client follows to get an access token. The right grant depends on two questions. First, is a user involved, or is the application acting for itself? Second, can the client keep a secret? A confidential client runs on a server we control and can store a client secret or private key. A public client runs on the user's device, such as JavaScript in a browser or a mobile app, and anything it contains can be extracted, so it cannot authenticate with a secret.
PKCE, Proof Key for Code Exchange, protects the authorization code flow. The client creates a random code_verifier, sends its SHA-256 hash as code_challenge when starting login, and must present the original verifier when exchanging the code for tokens. An attacker who intercepts the code cannot use it without the verifier.
| Case | Grant | Why |
|---|---|---|
| Server-rendered web app | Authorization code + PKCE, confidential | User present; server keeps the secret |
| Single-page app | Auth code + PKCE public, or a BFF | User present; browser cannot keep secrets |
| Nightly batch job | Client credentials | No user; the job acts as itself |
| Smart TV app | Device authorization grant | User present; no usable keyboard or browser |
| Implicit, password grants | Do not use | Tokens in URLs; app sees the password |
2Case 1: Server-Rendered Web App
The application runs on our server, and the user is in a browser. The authorization code flow fits exactly: the browser is redirected to the authorization server, the user signs in, the browser comes back with a short-lived code, and our server exchanges the code for tokens over a direct back-channel call, authenticating with its client secret. Tokens never pass through the browser; the user just gets a session cookie.
PKCE was originally designed for public clients, but current best practice recommends it for confidential clients as well, because it also defends against code injection attacks. Spring Security's oauth2Login() implements the whole flow; PKCE is applied automatically for public clients and can be switched on for confidential ones with an authorization request customizer; check your version for the exact option.
3Case 2: Single-Page App
A SPA is a public client: its code is downloaded to every user. It can still use the authorization code flow, with PKCE replacing the secret, and it then holds access and refresh tokens in the browser. That works, but any cross-site scripting bug can steal those tokens, and short lifetimes and refresh token rotation only limit the damage.
The preferred option for sensitive applications is a backend-for-frontend (BFF): a small server-side component, for example a Spring application with oauth2Login(), acts as the confidential client, holds the tokens and gives the SPA an HttpOnly session cookie. The SPA calls the BFF, which adds the access token when forwarding to APIs. Tokens never reach JavaScript, which removes the most common way they are stolen.
4Case 3: Nightly Batch Job
At 02:00 there is no user to log in. The job acts on its own authority, so it uses the client credentials grant: it authenticates directly to the token endpoint with its client id and secret (or, better, a private key JWT or mutual TLS) and receives an access token with the scopes granted to that client. In Spring, the OAuth2 client support fetches and caches the token for us.
The client gets only the scopes it needs, here reports.write, and its secret lives in a secret store, never in the repository. Recent Spring Security versions also provide an interceptor for RestClient that attaches the token for a named registration; check your version. Because the token belongs to the job, not to a person, the API's audit log shows nightly-batch as the actor, which is honest.
5Case 4: Smart TV App
A TV has a user but no comfortable way to type a password and often no full browser. The device authorization grant was designed for this. The TV asks the authorization server for a device code and a short user code, shows something like "Go to example.com/activate and enter WDJB-MJHT", and polls the token endpoint. The user opens that page on a phone, signs in with all the usual protections, such as passkeys or MFA, and approves. The TV's next poll returns the tokens.
6 messages in order. Play them, or step through them one at a time.
The password is typed only on the phone, never on the TV.
6Grants to Retire: Implicit and Password
The implicit grant returned the access token directly in the redirect URL fragment. It was invented for browser apps before PKCE and CORS made the code flow possible, and it exposes tokens in browser history and to any script on the page, with no refresh tokens. The resource owner password credentials grant has the application collect the user's username and password and send them to the token endpoint. That trains users to type passwords into applications, prevents MFA and federated login, and gives every client the user's credentials.
The OAuth 2.0 Security Best Current Practice deprecates both, and the OAuth 2.1 draft leaves them out entirely. Any existing use is a migration item: implicit to code with PKCE or a BFF, password grant to the code flow, or to client credentials when no user was really involved.
7Follow-up questions
Is the client credentials grant safe for a mobile app calling our API?
No. A mobile app is a public client, so any secret embedded in it can be extracted, and the token would represent the app, not the user. Mobile apps use the authorization code flow with PKCE through the system browser.
Why does the device flow poll instead of receiving a callback?
The TV has no address the authorization server can redirect to, and it may sit behind home routers. Polling at the interval the server specifies is simple and works everywhere; the server answers authorization_pending until the user approves.
Does PKCE replace the client secret for confidential clients?
No. They protect different things: the secret proves which client is calling, and PKCE proves the code exchange comes from the same client that started the login. Confidential clients use both.
8Conclusion
In this answer, we chose a grant by asking whether a user is present and whether the client can keep a secret. The server-rendered app uses the authorization code flow as a confidential client with PKCE, the SPA uses code with PKCE or preferably a backend-for-frontend, the batch job uses client credentials, and the smart TV uses the device authorization grant. The implicit and password grants are deprecated by current best practice and left out of the OAuth 2.1 draft.
Open with
I decide by two questions: is a user present, and can the client keep a secret. For the server-rendered web app, a user is present and the server can keep a secret, so it is the authorization code flow as a confidential client; tokens are exchanged on the back channel and I add PKCE, which current best practice recommends even for confidential clients.
Then, if they want more
For the single-page app, the user is present but the browser cannot keep secrets, so it is authorization code with PKCE as a public client, or, my preference, a backend-for-frontend that does the code flow on the server and gives the SPA only an HttpOnly session cookie, so tokens never reach JavaScript.
For the nightly batch job there is no user, so client credentials: the job authenticates as itself with its own narrow scopes, and Spring's OAuth2 client fetches and caches the token.
For the smart TV, the device authorization grant: the TV shows a short code and a URL, the user approves on their phone with normal MFA, and the TV polls for the tokens. The implicit grant and the resource owner password grant should no longer be used; current security best practice deprecates them and OAuth 2.1 drops them.
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
- 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 "Which OAuth2 grant would you use for each case and why: (1) a server-rendered web app...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/efbc1cef36/