PKCE dan Pengamanan Klien Publik
Pelajari cara ekstensi PKCE melindungi alur Kode Otorisasi OAuth2 untuk aplikasi seluler dan aplikasi satu halaman yang tidak dapat menyimpan rahasia klien.
PKCE dan Pengamanan Klien Publik adalah pelajaran Spring Security 6 & JWT Authentication gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Security 6 & JWT Authentication, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Security 6 & JWT Authentication mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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=S256Server 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_RANDOMServer 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_verifierand sends its hash as thecode_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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “PKCE dan Pengamanan Klien Publik” gratis?
Ya — teks lengkap “PKCE dan Pengamanan Klien Publik” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Security 6 & JWT Authentication, upgrade ke CoddyKit PRO. Kursus Spring Security 6 & JWT Authentication mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “PKCE dan Pengamanan Klien Publik”?
Pelajari cara ekstensi PKCE melindungi alur Kode Otorisasi OAuth2 untuk aplikasi seluler dan aplikasi satu halaman yang tidak dapat menyimpan rahasia klien. Kamu berlatih Spring Security 6 & JWT Authentication dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Spring Security 6 & JWT Authentication?
Tidak diperlukan pengalaman sebelumnya. Spring Security 6 & JWT Authentication di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “PKCE dan Pengamanan Klien Publik” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Security 6 & JWT Authentication ini?
Ya. Setiap pelajaran Spring Security 6 & JWT Authentication menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Ikhtisar Protokol OAuth2
- Pengantar OpenID Connect
- Jenis Pemberian OAuth2 yang Umum
- PKCE dan Pengamanan Klien Publik