Quick revision
The short answer
With PKCE, the client creates a random code_verifier, sends its hash as code_challenge = BASE64URL(SHA-256(verifier)) with method S256 in the authorization request, and later sends the verifier itself when it exchanges the code; the server hashes it and checks it matches. So an attacker who intercepts the authorization code cannot redeem it without the verifier. Spring Security's OAuth2 client uses PKCE automatically for public clients and can enable it for confidential clients, for example with OAuth2AuthorizationRequestCustomizers.withPkce(); defaults change between versions, so check yours.
What to remember
In this answer, we went through PKCE step by step. The client creates a random code_verifier, sends code_challenge, the base64url SHA-256 hash, with method S256, and later sends the verifier at the token endpoint, where the provider checks that its hash matches. An attacker who intercepts the code, through a malicious app or a log, cannot redeem it without the verifier. We saw why S256 beats plain, and that Spring Security uses PKCE automatically for public clients and can enable it for confidential ones with OAuth2AuthorizationRequestCustomizers.withPkce().
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.
With PKCE, the client creates a random code_verifier, sends its hash as code_challenge = BASE64URL(SHA-256(verifier)) with method S256 in the authorization request, and later sends the verifier itself when it exchanges the code; the server hashes it and checks it matches. So an attacker who intercepts the authorization code cannot redeem it without the verifier. Spring Security's OAuth2 client uses PKCE automatically for public clients and can enable it for confidential clients, for example with OAuth2AuthorizationRequestCustomizers.withPkce(); defaults change between versions, so check yours.
Why this question is asked
The authorization code flow sends a short-lived code through the browser, in a redirect URL, and that path is easier to observe than it looks: other apps on a phone, browser history, logs and proxies can all see URLs. PKCE, Proof Key for Code Exchange, defined in RFC 7636, makes a stolen code useless. Interviewers ask about it because it is now expected in every code flow, and because explaining it step by step shows we understand the flow itself. In this answer, we will recap the code flow, go through PKCE one step at a time with real values, see which attack it stops, and look at when Spring Security turns it on.
1A Quick Recap of the Authorization Code Flow
In the code flow, the client sends the user's browser to the identity provider's authorization endpoint. The user logs in, and the provider redirects the browser back to the client's redirect URI with a one-time authorization code in the URL. The client then exchanges that code at the token endpoint for tokens.
For a confidential client, a server that can keep a secret, the exchange includes the client secret. But a public client, such as a mobile app or a single-page app, cannot keep a secret: anything inside the app can be extracted. Without PKCE, anyone holding the code could redeem it, as long as they present the public client's id.
2The Client Creates a Verifier and a Challenge
Before redirecting, the client generates a code_verifier: a high-entropy random string of 43 to 128 characters. It keeps the verifier to itself, in memory or in the user's session. It then computes the code_challenge by hashing the verifier with SHA-256 and encoding the hash as base64url without padding. The values below are the worked example from RFC 7636.
Notice that the hash goes one way only. Anyone can see the challenge, but nobody can work back from it to the verifier.
3Step 2 and 3: Sending the Challenge, Then the Verifier
The authorization request carries the challenge and the method. The provider stores the challenge together with the code it issues.
11 messages in order. Play them, or step through them one at a time.
The attacker has the code but not the verifier, so the exchange fails.
When the code comes back to the token endpoint, it arrives with the verifier. The provider hashes the verifier, encodes it the same way, and compares the result with the challenge it stored for that code. If they match, it issues tokens; if not, it answers invalid_grant. The reason this works is that the verifier never travelled through the browser redirect. Only the challenge did.
4Which Attack PKCE Prevents
PKCE stops authorization code interception. The classic case is mobile: two apps on a phone register the same custom URL scheme, such as shopapp://callback, and the operating system hands the redirect, code included, to the malicious one. Other leaks come from URLs written to logs, browser history or referrer headers. In each case the attacker gets the code but never the verifier, because the verifier stayed inside the legitimate client.
It also binds the code to the browser session that started the flow, which helps against code injection, where an attacker pushes a stolen code into a victim's session; we cover that with confidential clients in another question.
5Why S256 and Not plain
RFC 7636 also defines a method called plain, where the challenge is simply the verifier itself. That only helps if the attacker can see the code but not the authorization request. Anyone who sees the request, through a log or a proxy, learns the verifier directly. S256 sends only a hash, so seeing the request reveals nothing useful. Plain exists for clients that cannot compute SHA-256, which today is practically none, so we use S256 and configure servers to reject plain.
6When Spring Security Uses PKCE
Spring Security's OAuth2 client adds PKCE automatically for a public client, a registration with client-authentication-method none and no secret. For confidential clients we can switch it on through the authorization request resolver, and newer versions add a per-registration setting to require it; the defaults have been moving toward PKCE everywhere, so check your version.
7Follow-up questions
Is PKCE a replacement for the state parameter?
Not entirely. state protects the client against CSRF on its callback, by checking that the response belongs to a request this browser started. PKCE protects the code exchange. Current guidance says PKCE can provide the CSRF protection too when the provider enforces it, but Spring sends both, and there is no reason to drop state.
Where does a single-page app keep the verifier?
In memory or sessionStorage for the few seconds between the redirect and the callback. It is short-lived and useless without the code, but the better design is still to run the flow in a backend-for-frontend so tokens never reach the browser.
Why must the verifier be long and random?
Because the challenge is public. A short or predictable verifier could be guessed by hashing candidates until one matches the challenge. With 32 random bytes, guessing is out of reach.
8Conclusion
In this answer, we went through PKCE step by step. The client creates a random code_verifier, sends code_challenge, the base64url SHA-256 hash, with method S256, and later sends the verifier at the token endpoint, where the provider checks that its hash matches. An attacker who intercepts the code, through a malicious app or a log, cannot redeem it without the verifier. We saw why S256 beats plain, and that Spring Security uses PKCE automatically for public clients and can enable it for confidential ones with OAuth2AuthorizationRequestCustomizers.withPkce().
Open with
PKCE adds a one-time secret to the code flow that never goes through the browser. Before redirecting, the client generates a random code_verifier, 32 random bytes encoded as 43 characters for example, and computes the code_challenge as the base64url encoding of the SHA-256 hash of the verifier.
Then, if they want more
It sends the challenge and the method S256 in the authorization request, and the identity provider stores the challenge with the code it issues. When the client exchanges the code at the token endpoint, it sends the verifier; the provider hashes it, compares it with the stored challenge, and only then issues tokens.
The attack it prevents is authorization code interception: on mobile a malicious app can register the same redirect scheme and receive the code, and codes can also leak into logs or history. The attacker has the code but not the verifier, so the exchange fails with invalid_grant.
The plain method should not be used, because the challenge would then equal the verifier and anyone seeing the request would learn it. In Spring Security, the OAuth2 client applies PKCE automatically for public clients without a secret, and for confidential clients I enable it by customising the authorization request resolver with OAuth2AuthorizationRequestCustomizers.withPkce(), checking the defaults of our version.
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 "Explain how PKCE works step by step and which attack it prevents. When does Spring...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/ccb73eb03c/