fix: 修正CORS配置 - 公网IP访问场景,暂无域名阶段允许所有来源
This commit is contained in:
+8
-11
@@ -15,21 +15,18 @@ const imageFormats = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg'];
|
||||
const app = express();
|
||||
const PORT = process.argv[2] || process.env.PORT || 3000;
|
||||
|
||||
// 中间件 - CORS配置(支持内网访问)
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'];
|
||||
const isPrivateIP = (origin) => {
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const host = new URL(origin).hostname;
|
||||
return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host);
|
||||
} catch { return false; }
|
||||
};
|
||||
// 中间件 - CORS配置
|
||||
// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
|
||||
// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
||||
const corsOptions = {
|
||||
origin: (origin, callback) => {
|
||||
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) {
|
||||
// 暂无域名阶段:允许所有来源访问(公网IP访问需要)
|
||||
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, true); // 内网使用,允许所有来源
|
||||
// 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
|
||||
callback(null, true);
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
|
||||
+8
-11
@@ -8,21 +8,18 @@ dotenv.config();
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3002;
|
||||
|
||||
// 中间件 - CORS配置(支持内网访问)
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'];
|
||||
const isPrivateIP = (origin) => {
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const host = new URL(origin).hostname;
|
||||
return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host);
|
||||
} catch { return false; }
|
||||
};
|
||||
// 中间件 - CORS配置
|
||||
// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
|
||||
// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
||||
const corsOptions = {
|
||||
origin: (origin, callback) => {
|
||||
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) {
|
||||
// 暂无域名阶段:允许所有来源访问(公网IP访问需要)
|
||||
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, true); // 内网使用,允许所有来源
|
||||
// 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
|
||||
callback(null, true);
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
|
||||
+8
-11
@@ -8,21 +8,18 @@ dotenv.config();
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3002;
|
||||
|
||||
// 中间件 - CORS配置(支持内网访问)
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'];
|
||||
const isPrivateIP = (origin) => {
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const host = new URL(origin).hostname;
|
||||
return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/. test(host);
|
||||
} catch { return false; }
|
||||
};
|
||||
// 中间件 - CORS配置
|
||||
// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
|
||||
// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
||||
const corsOptions = {
|
||||
origin: (origin, callback) => {
|
||||
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) {
|
||||
// 暂无域名阶段:允许所有来源访问(公网IP访问需要)
|
||||
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, true); // 内网使用,允许所有来源
|
||||
// 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
|
||||
callback(null, true);
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
|
||||
+8
-11
@@ -8,21 +8,18 @@ dotenv.config();
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3002;
|
||||
|
||||
// 中间件 - CORS配置(支持内网访问)
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'];
|
||||
const isPrivateIP = (origin) => {
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const host = new URL(origin).hostname;
|
||||
return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host);
|
||||
} catch { return false; }
|
||||
};
|
||||
// 中间件 - CORS配置
|
||||
// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
|
||||
// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
||||
const corsOptions = {
|
||||
origin: (origin, callback) => {
|
||||
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) {
|
||||
// 暂无域名阶段:允许所有来源访问(公网IP访问需要)
|
||||
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, true); // 内网使用,允许所有来源
|
||||
// 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
|
||||
callback(null, true);
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
|
||||
@@ -28,21 +28,18 @@ dotenv.config();
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3002;
|
||||
|
||||
// 中间件 - CORS配置(支持内网访问)
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'];
|
||||
const isPrivateIP = (origin) => {
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const host = new URL(origin).hostname;
|
||||
return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host);
|
||||
} catch { return false; }
|
||||
};
|
||||
// 中间件 - CORS配置
|
||||
// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
|
||||
// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
||||
const corsOptions = {
|
||||
origin: (origin, callback) => {
|
||||
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) {
|
||||
// 暂无域名阶段:允许所有来源访问(公网IP访问需要)
|
||||
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, true); // 内网使用,允许所有来源
|
||||
// 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
|
||||
callback(null, true);
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
|
||||
@@ -9,21 +9,18 @@ const financeRouter = require('./finance-api');
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 5000;
|
||||
|
||||
// 中间件 - CORS配置(支持内网访问)
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'];
|
||||
const isPrivateIP = (origin) => {
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const host = new URL(origin).hostname;
|
||||
return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host);
|
||||
} catch { return false; }
|
||||
};
|
||||
// 中间件 - CORS配置
|
||||
// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
|
||||
// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
||||
const corsOptions = {
|
||||
origin: (origin, callback) => {
|
||||
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) {
|
||||
// 暂无域名阶段:允许所有来源访问(公网IP访问需要)
|
||||
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, true); // 内网使用,允许所有来源
|
||||
// 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
|
||||
callback(null, true);
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
|
||||
@@ -8,21 +8,18 @@ const db = require('./db');
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3002;
|
||||
|
||||
// 中间件 - CORS配置(支持内网访问)
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'];
|
||||
const isPrivateIP = (origin) => {
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const host = new URL(origin).hostname;
|
||||
return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host);
|
||||
} catch { return false; }
|
||||
};
|
||||
// 中间件 - CORS配置
|
||||
// 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
|
||||
// TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
|
||||
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
||||
const corsOptions = {
|
||||
origin: (origin, callback) => {
|
||||
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) {
|
||||
// 暂无域名阶段:允许所有来源访问(公网IP访问需要)
|
||||
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(null, true); // 内网使用,允许所有来源
|
||||
// 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
|
||||
callback(null, true);
|
||||
}
|
||||
},
|
||||
credentials: true,
|
||||
|
||||
Reference in New Issue
Block a user