From bbacf48dcf9f2f47e2b25cce404db5c6f94c0223 Mon Sep 17 00:00:00 2001 From: kicer Date: Sat, 5 Sep 2026 16:15:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=90=8E=E4=BC=9A=E8=AF=9D=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E9=94=99=E8=AF=AF=E7=94=A8=E6=88=B7=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=9A=84cookie=E6=AE=8B=E7=95=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 登录成功后将凭据存入sessionStorage - req()自动附加Authorization:Basic header避免依赖易污染的cookie - logout同步清除前端凭据和服务端cookie --- app/server/public/index.html | 9 ++++++++- app/server/public/login.html | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/server/public/index.html b/app/server/public/index.html index 681e850..00a8939 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -404,6 +404,9 @@ // ---------- API(与 core.js 的 req 保持一致) ---------- async function req(method, url, body, isForm) { const opt = { method, headers: {} }; + // 从 sessionStorage 取登录凭据,自动加 Basic auth 头,确保 cookie 残留时认证也不出错 + const creds = sessionStorage.getItem('agent_creds'); + if (creds) opt.headers['Authorization'] = 'Basic ' + creds; if (body && !isForm) { opt.headers['Content-Type'] = 'application/json'; opt.body = JSON.stringify(body); @@ -3320,7 +3323,11 @@ window.sUI = { // 账户 changePassword: changePassword, - logout: function () { location.href = '/logout'; }, + logout: async function () { + sessionStorage.removeItem('agent_creds'); // 清凭据,避免下次访问仍走 Basic auth + try { await fetch('/api/logout', { method: 'POST' }); } catch (_) {} + location.href = '/logout'; + }, // 服务 saveForm: saveForm, restart: restart, diff --git a/app/server/public/login.html b/app/server/public/login.html index ac385b0..0f0e19f 100644 --- a/app/server/public/login.html +++ b/app/server/public/login.html @@ -87,7 +87,12 @@ throw new Error(msg); } const j = await res.json().catch(() => ({})); - if (j && j.token) localStorage.setItem(TOKEN_KEY, j.token); + if (j && j.token) { + localStorage.setItem(TOKEN_KEY, j.token); + // 持久化凭据供 req() 使用 Basic auth,避免 cookie 残留导致切换用户后认证错乱 + const creds = btoa(encodeURIComponent((document.getElementById('login-user').value || '') + ':' + (document.getElementById('login-pass').value || ''))); + sessionStorage.setItem('agent_creds', creds); + } location.replace('/'); // 替换而非压栈:历史里不留 /login 条目,登录后侧滑不会退回登录页 } catch (e2) { errEl.textContent = e2.message || '登录失败';