보안설정 수정, 모바일 최적화 변경

This commit is contained in:
2025-06-11 03:33:44 +09:00
parent d085e037a1
commit 48f1bf1c52
10 changed files with 339 additions and 141 deletions
+46 -6
View File
@@ -25,7 +25,29 @@ const io = socketIo(server, {
}
});
app.use(cors());
function getDomain(origin) {
try {
const url = new URL(origin);
return url.hostname; // 'lanebow.com', 'localhost'
} catch (e) {
return null;
}
}
const allowedDomains = ['lanebow.com', 'localhost'];
app.use(cors({
origin: function(origin, callback) {
if (!origin) return callback(null, true);
const domain = getDomain(origin);
if (allowedDomains.includes(domain)) {
return callback(null, true);
} else {
return callback(new Error('Not allowed by CORS'), false);
}
},
credentials: true
}));
app.use(bodyParser.json());
// Socket.IO 연결 처리
@@ -82,17 +104,35 @@ const options = {
// MySQL 세션 저장소 생성
const sessionStore = new MySQLStore(options);
app.use(session({
const baseSessionOptions = {
key: 'bowling_session',
secret: process.env.JWT_SECRET,
store: sessionStore,
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production',
maxAge: 24 * 60 * 60 * 1000 // 1일
cookie: {
maxAge: 24 * 60 * 60 * 1000,
secure: false,
sameSite: 'lax'
}
}));
};
app.use(session(baseSessionOptions));
// 요청마다 secure/sameSite를 동적으로 할당
app.use((req, res, next) => {
const origin = req.headers.origin;
const domain = getDomain(origin);
if (req.session && req.session.cookie) {
if (domain === 'lanebow.com') {
req.session.cookie.secure = true;
req.session.cookie.sameSite = 'none';
} else {
req.session.cookie.secure = false;
req.session.cookie.sameSite = 'lax';
}
}
next();
});
// 데이터베이스 연결 및 모델 동기화 실행
sequelize.authenticate()