修复crsf问题

This commit is contained in:
leijiaping
2026-04-03 10:33:49 +08:00
parent 15275b4ff3
commit 08c7ea1ade
3 changed files with 6523 additions and 0 deletions

6516
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

4
src/lib/http/csrf.ts Normal file
View File

@@ -0,0 +1,4 @@
export const getCsrfToken = (): string | null => {
const match = document.cookie.match(/(?:^|; )XSRF-TOKEN=([^;]*)/);
return match ? decodeURIComponent(match[1]) : null;
};

View File

@@ -2,6 +2,7 @@ import { useUserStore } from "@/stores/user";
import type { AxiosInstance, InternalAxiosRequestConfig } from "axios"; import type { AxiosInstance, InternalAxiosRequestConfig } from "axios";
import { toast } from "sonner"; import { toast } from "sonner";
import router from "@/router"; import router from "@/router";
import { getCsrfToken } from "./csrf";
export const downloadFile = (res: any) => { export const downloadFile = (res: any) => {
const [, filename] = res.headers["content-disposition"]?.split(";"); const [, filename] = res.headers["content-disposition"]?.split(";");
@@ -26,11 +27,13 @@ export const setupInterceptors = (instance: AxiosInstance) => {
"background:#4f46e5;color:#fff;", "background:#4f46e5;color:#fff;",
config, config,
); );
const csrfToken = getCsrfToken();
// @ts-ignore // @ts-ignore
config.headers = { config.headers = {
...config.headers, ...config.headers,
// @ts-ignore // @ts-ignore
Authorization: `Bearer ${useUserStore.getState().token}`, Authorization: `Bearer ${useUserStore.getState().token}`,
"X-XSRF-TOKEN": csrfToken || "",
}; };
return config; return config;
}, },