Spring Security 6 & JWT Authentication · Lezione

PKCE e protezione dei client pubblici

Impari come l'estensione PKCE protegge il flusso OAuth2 Authorization Code per le app mobili e le single-page app che non possono custodire un client secret.

Lezione 4 di 413 passaggi

PKCE e protezione dei client pubblici è una lezione Spring Security 6 & JWT Authentication gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Security 6 & JWT Authentication, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Security 6 & JWT Authentication include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

The Public Client Problem

Mobile apps and SPAs are public clients: their code ships to the user, so they cannot safely store a client secret. Without a secret, the plain Authorization Code flow is vulnerable to interception.

What PKCE Solves

PKCE (Proof Key for Code Exchange, pronounced 'pixy') adds a dynamic secret per authorization request. Even if the authorization code is stolen, it cannot be exchanged without the matching proof.

The Code Verifier

The client generates a random, high-entropy string called the code_verifier and keeps it in memory for this one flow.

const codeVerifier = base64url(randomBytes(32));

The Code Challenge

The client hashes the verifier with SHA-256 to make the code_challenge. The hash is sent to the server, but the original verifier never leaves the device yet.

const codeChallenge = base64url(sha256(codeVerifier));

Starting the Authorization Request

The client sends the challenge and the method (S256) to the authorization endpoint along with the usual parameters.

GET /authorize?response_type=code
  &client_id=app123
  &code_challenge=XYZ...
  &code_challenge_method=S256

Server Stores the Challenge

The authorization server remembers the code_challenge and links it to the authorization code it issues after the user logs in.

Exchanging the Code

When swapping the code for tokens, the client now reveals the original code_verifier.

POST /token
  grant_type=authorization_code
  &code=abc123
  &code_verifier=ORIGINAL_RANDOM

Server Verifies the Proof

The server hashes the received verifier and compares it to the stored challenge. If they match, the requester is the same party that started the flow; otherwise it rejects the exchange.

if (sha256(received_verifier) !== stored_challenge) {
  reject('invalid_grant');
}

Why It Stops Interception

An attacker who steals the authorization code (for example via a malicious app on the device) still cannot use it: they never saw the code_verifier, which existed only inside the legitimate client's memory.

PKCE Is Now Recommended for All

Originally for mobile, PKCE is now recommended for every Authorization Code flow, including confidential web clients. OAuth 2.1 makes it the default.

Library Support

You rarely implement PKCE by hand. Libraries like AppAuth, oidc-client-ts, and Spring Authorization Server handle verifier generation, hashing, and validation for you.

Quick Check

Test your understanding of PKCE.

Recap

You learned how PKCE secures public clients:

  • Public clients cannot keep a secret, so the plain code flow is unsafe
  • The client creates a code_verifier and sends its hash as the code_challenge
  • The verifier is revealed only at token exchange
  • A stolen code is useless without the verifier

PKCE is now the default for all Authorization Code flows.

Gratis per iniziare

Impara Java con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «PKCE e protezione dei client pubblici» è gratuita?

Sì — il testo completo di «PKCE e protezione dei client pubblici» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Security 6 & JWT Authentication, passa a CoddyKit PRO. Il corso Spring Security 6 & JWT Authentication include 4 lezioni in totale.

Cosa imparerò in «PKCE e protezione dei client pubblici»?

Impari come l'estensione PKCE protegge il flusso OAuth2 Authorization Code per le app mobili e le single-page app che non possono custodire un client secret. Eserciti Spring Security 6 & JWT Authentication con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Spring Security 6 & JWT Authentication?

Non è richiesta alcuna esperienza precedente. Spring Security 6 & JWT Authentication su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «PKCE e protezione dei client pubblici»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Spring Security 6 & JWT Authentication?

Sì. Ogni lezione Spring Security 6 & JWT Authentication include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Panoramica del protocollo OAuth2
  2. Introduzione a OpenID Connect
  3. Tipi comuni di grant OAuth2
  4. PKCE e protezione dei client pubblici
← Torna a Spring Security 6 & JWT Authentication