From d2f782e47e34e2f3940d5b62a2650e26ee845a6a Mon Sep 17 00:00:00 2001 From: plf1996 Date: Thu, 18 Jun 2026 16:02:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20API=20=E8=AF=B7=E6=B1=82=E5=89=8D?= =?UTF-8?q?=E7=94=A8=20keycloak.updateToken(30)=20=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E5=8D=B3=E5=B0=86=E8=BF=87=E6=9C=9F=E7=9A=84=20token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getToken() 只返回缓存的 token,可能已过期(默认 5 分钟生命周期)。 updateToken(30) 确保发送前 token 至少还有 30 秒有效时长。 --- frontend/src/app/api.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/api.ts b/frontend/src/app/api.ts index 20da8f6..0074de9 100644 --- a/frontend/src/app/api.ts +++ b/frontend/src/app/api.ts @@ -32,11 +32,13 @@ async function request( if (parts.length) url += '?' + parts.join('&'); } - // Build headers with optional Bearer token + // Build headers with optional Bearer token — refresh if near expiry const headers: Record = {}; try { - const { getToken } = await import('./auth'); - const token = getToken(); + const mod = await import('./auth'); + // Ensure token is valid for at least 30s before sending + await mod.default.updateToken(30); + const token = mod.getToken(); if (token) headers['Authorization'] = `Bearer ${token}`; } catch { /* auth not initialized */ } if (!options.formData && options.body !== undefined) {