fix: 修正CORS配置 - 公网IP访问场景,暂无域名阶段允许所有来源

This commit is contained in:
root
2026-04-19 19:40:40 +08:00
parent e2d7f004f8
commit 1cad1e438d
7 changed files with 56 additions and 77 deletions
+8 -11
View File
@@ -15,21 +15,18 @@ const imageFormats = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg'];
const app = express(); const app = express();
const PORT = process.argv[2] || process.env.PORT || 3000; const PORT = process.argv[2] || process.env.PORT || 3000;
// 中间件 - CORS配置(支持内网访问) // 中间件 - CORS配置
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; // 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
const isPrivateIP = (origin) => { // TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
if (!origin) return false; const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
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; }
};
const corsOptions = { const corsOptions = {
origin: (origin, callback) => { origin: (origin, callback) => {
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { // 暂无域名阶段:允许所有来源访问(公网IP访问需要)
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
callback(null, true); callback(null, true);
} else { } else {
callback(null, true); // 内网使用,允许所有来源 // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
callback(null, true);
} }
}, },
credentials: true, credentials: true,
+8 -11
View File
@@ -8,21 +8,18 @@ dotenv.config();
const app = express(); const app = express();
const PORT = process.env.PORT || 3002; const PORT = process.env.PORT || 3002;
// 中间件 - CORS配置(支持内网访问) // 中间件 - CORS配置
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; // 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
const isPrivateIP = (origin) => { // TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
if (!origin) return false; const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
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; }
};
const corsOptions = { const corsOptions = {
origin: (origin, callback) => { origin: (origin, callback) => {
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { // 暂无域名阶段:允许所有来源访问(公网IP访问需要)
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
callback(null, true); callback(null, true);
} else { } else {
callback(null, true); // 内网使用,允许所有来源 // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
callback(null, true);
} }
}, },
credentials: true, credentials: true,
+8 -11
View File
@@ -8,21 +8,18 @@ dotenv.config();
const app = express(); const app = express();
const PORT = process.env.PORT || 3002; const PORT = process.env.PORT || 3002;
// 中间件 - CORS配置(支持内网访问) // 中间件 - CORS配置
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; // 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
const isPrivateIP = (origin) => { // TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
if (!origin) return false; const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
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; }
};
const corsOptions = { const corsOptions = {
origin: (origin, callback) => { origin: (origin, callback) => {
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { // 暂无域名阶段:允许所有来源访问(公网IP访问需要)
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
callback(null, true); callback(null, true);
} else { } else {
callback(null, true); // 内网使用,允许所有来源 // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
callback(null, true);
} }
}, },
credentials: true, credentials: true,
+8 -11
View File
@@ -8,21 +8,18 @@ dotenv.config();
const app = express(); const app = express();
const PORT = process.env.PORT || 3002; const PORT = process.env.PORT || 3002;
// 中间件 - CORS配置(支持内网访问) // 中间件 - CORS配置
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; // 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
const isPrivateIP = (origin) => { // TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
if (!origin) return false; const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
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; }
};
const corsOptions = { const corsOptions = {
origin: (origin, callback) => { origin: (origin, callback) => {
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { // 暂无域名阶段:允许所有来源访问(公网IP访问需要)
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
callback(null, true); callback(null, true);
} else { } else {
callback(null, true); // 内网使用,允许所有来源 // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
callback(null, true);
} }
}, },
credentials: true, credentials: true,
+8 -11
View File
@@ -28,21 +28,18 @@ dotenv.config();
const app = express(); const app = express();
const PORT = process.env.PORT || 3002; const PORT = process.env.PORT || 3002;
// 中间件 - CORS配置(支持内网访问) // 中间件 - CORS配置
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; // 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
const isPrivateIP = (origin) => { // TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
if (!origin) return false; const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
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; }
};
const corsOptions = { const corsOptions = {
origin: (origin, callback) => { origin: (origin, callback) => {
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { // 暂无域名阶段:允许所有来源访问(公网IP访问需要)
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
callback(null, true); callback(null, true);
} else { } else {
callback(null, true); // 内网使用,允许所有来源 // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
callback(null, true);
} }
}, },
credentials: true, credentials: true,
+8 -11
View File
@@ -9,21 +9,18 @@ const financeRouter = require('./finance-api');
const app = express(); const app = express();
const PORT = process.env.PORT || 5000; const PORT = process.env.PORT || 5000;
// 中间件 - CORS配置(支持内网访问) // 中间件 - CORS配置
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; // 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
const isPrivateIP = (origin) => { // TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
if (!origin) return false; const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
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; }
};
const corsOptions = { const corsOptions = {
origin: (origin, callback) => { origin: (origin, callback) => {
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { // 暂无域名阶段:允许所有来源访问(公网IP访问需要)
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
callback(null, true); callback(null, true);
} else { } else {
callback(null, true); // 内网使用,允许所有来源 // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
callback(null, true);
} }
}, },
credentials: true, credentials: true,
+8 -11
View File
@@ -8,21 +8,18 @@ const db = require('./db');
const app = express(); const app = express();
const PORT = process.env.PORT || 3002; const PORT = process.env.PORT || 3002;
// 中间件 - CORS配置(支持内网访问) // 中间件 - CORS配置
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001']; // 当前暂无域名,员工通过公网IP访问,暂时允许所有来源
const isPrivateIP = (origin) => { // TODO: 申请域名后,在 .env 的 CORS_ORIGIN 中填写域名,并切换为严格模式
if (!origin) return false; const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
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; }
};
const corsOptions = { const corsOptions = {
origin: (origin, callback) => { origin: (origin, callback) => {
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) { // 暂无域名阶段:允许所有来源访问(公网IP访问需要)
if (!origin || corsWhitelist.length === 0 || corsWhitelist.includes(origin)) {
callback(null, true); callback(null, true);
} else { } else {
callback(null, true); // 内网使用,允许所有来源 // 有域名后可改为 callback(new Error('Not allowed'), false) 限制来源
callback(null, true);
} }
}, },
credentials: true, credentials: true,