Quick revision
The short answer
Yes, the reviewer is right. Current OAuth security best practice and the OAuth 2.1 draft recommend PKCE for every client that uses the code flow, confidential ones included, because a client secret proves which client redeems a code but not which browser session started the flow, so it does not stop authorization code injection. In Spring Security we enable it by customising the OAuth2AuthorizationRequestResolver with OAuth2AuthorizationRequestCustomizers.withPkce(), then confirm the provider supports S256 and the token request carries the verifier.
What to remember
In this answer, we saw that the reviewer is right. A client secret proves who redeems a code but not which session asked for it, so a stolen code injected into an attacker's session would be redeemed by our own app. PKCE binds each code to the session that started the flow, which is why current best practice and OAuth 2.1 ask for it in every code flow. We enabled it with OAuth2AuthorizationRequestCustomizers.withPkce(), verified S256 support and the verifier in the token request, and saw how Spring Authorization Server can require it per client.
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.
Yes, the reviewer is right. Current OAuth security best practice and the OAuth 2.1 draft recommend PKCE for every client that uses the code flow, confidential ones included, because a client secret proves which client redeems a code but not which browser session started the flow, so it does not stop authorization code injection. In Spring Security we enable it by customising the OAuth2AuthorizationRequestResolver with OAuth2AuthorizationRequestCustomizers.withPkce(), then confirm the provider supports S256 and the token request carries the verifier.
Why this question is asked
A finding like this often meets resistance: we already have a secret, the server is not a mobile app, so why add PKCE? Interviewers ask it because the answer requires understanding what the secret actually protects and what it does not. In this answer, we will look at what the secret proves, walk through the code injection attack that slips past it, see why the guidance now asks for PKCE everywhere, enable it in Spring Security, verify it end to end, and see how Spring Authorization Server can require it.
1What the Client Secret Proves
A confidential client is an application that runs on a server and can keep a secret, for example a Spring MVC web app with oauth2Login. When it exchanges an authorization code for tokens, it authenticates with its client secret. So the identity provider knows that the party redeeming the code is our application and not someone else.
But notice what that leaves open. The secret proves who redeems the code. It says nothing about whether the code belongs to the login that this particular browser session started. Our application redeems any code that arrives at its callback URL, because it has the secret.
2The Attack the Secret Does Not Stop
That gap enables authorization code injection. An attacker obtains a code issued to a victim for our client, perhaps from a leaked log, a referrer header or a compromised network device. The attacker cannot redeem it directly, because they do not have our secret. Instead, they start a login on our site in their own browser and, when our app expects the callback, they hand it the victim's code. Our application dutifully redeems it with its own secret, receives the victim's tokens, and logs the attacker in as the victim.
6 messages in order. Play them, or step through them one at a time.
The secret is used by our own app, so it cannot tell a stolen code from a real one.
The state parameter does not help here, because the attacker uses their own valid state from their own session. With PKCE, our app generated a verifier for the attacker's session and sends it when redeeming. The victim's code was bound to a different challenge, from the victim's session, so the provider rejects the exchange. PKCE ties each code to the session that asked for it, which is exactly the property the secret lacks.
3What the Guidance Says
The OAuth 2.0 Security Best Current Practice (RFC 9700) recommends PKCE for confidential clients as well as public ones, and the OAuth 2.1 draft makes PKCE part of the code flow for all clients. The OpenID Connect nonce gives similar protection for ID tokens when it is checked properly, but PKCE also protects the access token exchange and is simpler to get right. So the reviewer is right, and the cost is small: a few bytes in two requests.
4Enabling PKCE in Spring Security
Spring Security applies PKCE automatically only to public clients. For our confidential client, we give oauth2Login a customised authorization request resolver.
The customizer generates a verifier per login, keeps it with the saved authorization request in the session, adds code_challenge and code_challenge_method=S256 to the redirect, and the token request then includes the code_verifier alongside the client secret. Newer Spring Security versions also offer a per-registration setting to require PKCE, and defaults have been moving toward PKCE for all clients, so check your version.
5Verifying It End to End
Configuration is only half the job. First, we check that the provider supports S256: its discovery document at /.well-known/openid-configuration should list it in code_challenge_methods_supported. Second, we watch one login in a test environment, with the browser's developer tools for the redirect and debug logging for the token request, and confirm that the authorization request carries code_challenge and the token request carries code_verifier. Third, we ask whether the provider enforces PKCE for this client. A provider that accepts the challenge but also accepts exchanges without a verifier gives much weaker protection, so where possible we make it required on the provider side.
If we run our own Spring Authorization Server, that last step is one setting on the registered client: ClientSettings.builder().requireProofKey(true). Then any code request for that client without a challenge is refused, and any exchange without the matching verifier fails.
6Follow-up questions
Doesn't the nonce in OpenID Connect already prevent this?
The nonce binds the ID token to the session if the client checks it, and Spring Security does. But it protects only the ID token, and only when the provider includes it correctly. PKCE protects the code exchange itself, including access tokens for APIs, and is checked by the provider, so it is the more general defence.
Does PKCE replace the client secret for a confidential client?
No. They answer different questions: the secret authenticates the client, PKCE binds the code to the session. A confidential client should use both.
What if the identity provider does not support PKCE?
Then we record it as a risk, rely on state and nonce checks, keep codes short-lived and single-use, and push the provider to add support, since nearly all current providers support S256.
7Conclusion
In this answer, we saw that the reviewer is right. A client secret proves who redeems a code but not which session asked for it, so a stolen code injected into an attacker's session would be redeemed by our own app. PKCE binds each code to the session that started the flow, which is why current best practice and OAuth 2.1 ask for it in every code flow. We enabled it with OAuth2AuthorizationRequestCustomizers.withPkce(), verified S256 support and the verifier in the token request, and saw how Spring Authorization Server can require it per client.
Open with
Yes, the reviewer is right, and it is current best practice: the OAuth security BCP and the OAuth 2.1 draft recommend PKCE for every client using the code flow, including confidential ones.
Then, if they want more
The client secret only proves that our application is the one redeeming the code. It does not prove that the code belongs to the login this browser session started.
In a code injection attack, someone steals a victim's code, starts their own login on our site and hands the stolen code to our callback; our app redeems it with its own secret and logs the attacker in as the victim, and state does not help because the attacker uses their own valid state.
With PKCE, our app sends the verifier generated for the attacker's session, which does not match the challenge bound to the victim's code, so the provider refuses it. In Spring Security, PKCE is automatic only for public clients, so I create a DefaultOAuth2AuthorizationRequestResolver, set OAuth2AuthorizationRequestCustomizers.withPkce() on it and plug it into oauth2Login.
Then I verify: the provider's discovery document lists S256, the redirect carries code_challenge and the token request carries code_verifier. If we run Spring Authorization Server, I set requireProofKey in the client settings so it is enforced.
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 "A security review says your confidential server-side web client must use PKCE even...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/904781f975/