feat: SSO 쿠키 인증 통합 + 서브도메인 라우팅 아키텍처

- Path-based 라우팅을 서브도메인 기반으로 전환
  (tkfb/tkreport/tkqc.technicalkorea.net)
- 3개 시스템 프론트엔드에 SSO 쿠키 인증 통합
  (domain=.technicalkorea.net, localStorage 폴백)
- Gateway: 포털+로그인+System1 프록시, 쿠키 SSO 설정
- System 1: 토큰키 통일, nginx.conf 생성, 신고페이지 리다이렉트
- System 2: api-base.js/app-init.js 생성, getSSOToken() 통합
- System 3: TokenManager 쿠키 지원, 중앙 로그인 리다이렉트
- docker-compose.yml에 cloudflared 서비스 추가
- DEPLOY-GUIDE.md 배포 가이드 작성

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-02-09 18:41:44 +09:00
parent 550633b89d
commit 6495b8af32
114 changed files with 1729 additions and 4335 deletions

View File

@@ -98,16 +98,39 @@
</div>
<script>
// SSO 쿠키 유틸리티
var ssoCookie = {
set: function(name, value, days) {
var cookie = name + '=' + encodeURIComponent(value) + '; path=/';
if (days) cookie += '; max-age=' + (days * 86400);
if (window.location.hostname.includes('technicalkorea.net')) {
cookie += '; domain=.technicalkorea.net; secure; samesite=lax';
}
document.cookie = cookie;
},
get: function(name) {
var match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
return match ? decodeURIComponent(match[1]) : null;
},
remove: function(name) {
var cookie = name + '=; path=/; max-age=0';
if (window.location.hostname.includes('technicalkorea.net')) {
cookie += '; domain=.technicalkorea.net';
}
document.cookie = cookie;
}
};
async function handleLogin(e) {
e.preventDefault();
const btn = document.getElementById('submitBtn');
const errEl = document.getElementById('errorMsg');
var btn = document.getElementById('submitBtn');
var errEl = document.getElementById('errorMsg');
errEl.style.display = 'none';
btn.disabled = true;
btn.textContent = '로그인 중...';
try {
const res = await fetch('/auth/login', {
var res = await fetch('/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
@@ -116,21 +139,28 @@
})
});
const data = await res.json();
var data = await res.json();
if (!res.ok || !data.success) {
throw new Error(data.error || '로그인에 실패했습니다');
}
// 토큰과 유저 정보 저장
// 쿠키에 토큰 저장 (서브도메인 공유)
ssoCookie.set('sso_token', data.access_token, 7);
ssoCookie.set('sso_user', JSON.stringify(data.user), 7);
if (data.refresh_token) {
ssoCookie.set('sso_refresh_token', data.refresh_token, 30);
}
// localStorage에도 저장 (같은 도메인 내 호환성)
localStorage.setItem('sso_token', data.access_token);
localStorage.setItem('sso_user', JSON.stringify(data.user));
if (data.refresh_token) {
localStorage.setItem('sso_refresh_token', data.refresh_token);
}
// 포털로 이동
const redirect = new URLSearchParams(location.search).get('redirect');
// redirect 파라미터가 있으면 해당 URL로, 없으면 포털로
var redirect = new URLSearchParams(location.search).get('redirect');
window.location.href = redirect || '/';
} catch (err) {
errEl.textContent = err.message;
@@ -141,9 +171,11 @@
}
}
// 이미 로그인 되어있으면 포털로
if (localStorage.getItem('sso_token')) {
window.location.href = '/';
// 이미 로그인 되어있으면 포털로 (쿠키 또는 localStorage 체크)
var existingToken = ssoCookie.get('sso_token') || localStorage.getItem('sso_token');
if (existingToken && existingToken !== 'undefined' && existingToken !== 'null') {
var redirect = new URLSearchParams(location.search).get('redirect');
window.location.href = redirect || '/';
}
</script>
</body>

View File

@@ -141,19 +141,19 @@
<p>사용할 시스템을 선택하세요</p>
</div>
<div class="systems">
<a href="/factory/" class="system-card s1" id="card-s1">
<a href="/pages/dashboard.html" class="system-card s1" id="card-s1">
<div class="system-icon">&#127981;</div>
<h3>공장관리</h3>
<p>작업보고, 근태관리, TBM, 순회점검, 장비관리 등 현장 운영 전반</p>
<span class="badge">System 1</span>
</a>
<a href="/report/" class="system-card s2" id="card-s2">
<a id="card-s2-link" class="system-card s2" id="card-s2">
<div class="system-icon">&#128680;</div>
<h3>신고 시스템</h3>
<p>안전/부적합 이슈 신고, 처리현황 추적, 부적합 자동 연동</p>
<span class="badge">System 2</span>
</a>
<a href="/nc/" class="system-card s3" id="card-s3">
<a id="card-s3-link" class="system-card s3" id="card-s3">
<div class="system-icon">&#128203;</div>
<h3>부적합 관리</h3>
<p>부적합 이슈 접수, 처리, 리포트 생성, 프로젝트별 현황 관리</p>
@@ -167,12 +167,31 @@
<script src="/shared/nav-header.js"></script>
<script>
const TOKEN_KEY = 'sso_token';
const USER_KEY = 'sso_user';
var ssoCookie = {
get: function(name) {
var match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
return match ? decodeURIComponent(match[1]) : null;
},
remove: function(name) {
var cookie = name + '=; path=/; max-age=0';
if (window.location.hostname.includes('technicalkorea.net')) {
cookie += '; domain=.technicalkorea.net';
}
document.cookie = cookie;
}
};
function getToken() {
return ssoCookie.get('sso_token') || localStorage.getItem('sso_token');
}
function getUser() {
var raw = ssoCookie.get('sso_user') || localStorage.getItem('sso_user');
try { return JSON.parse(raw); } catch(e) { return null; }
}
function init() {
const token = localStorage.getItem(TOKEN_KEY);
const user = JSON.parse(localStorage.getItem(USER_KEY) || 'null');
var token = getToken();
var user = getUser();
if (token && user) {
showDashboard(user);
@@ -199,12 +218,36 @@
}
function logout() {
localStorage.removeItem(TOKEN_KEY);
localStorage.removeItem(USER_KEY);
fetch('/auth/logout', { method: 'POST' }).catch(() => {});
ssoCookie.remove('sso_token');
ssoCookie.remove('sso_user');
ssoCookie.remove('sso_refresh_token');
localStorage.removeItem('sso_token');
localStorage.removeItem('sso_user');
localStorage.removeItem('sso_refresh_token');
fetch('/auth/logout', { method: 'POST' }).catch(function(){});
location.reload();
}
// 서브도메인 링크 설정
function setupSystemLinks() {
var hostname = window.location.hostname;
var protocol = window.location.protocol;
var s2Link, s3Link;
if (hostname.includes('technicalkorea.net')) {
s2Link = protocol + '//tkreport.technicalkorea.net';
s3Link = protocol + '//tkqc.technicalkorea.net';
} else {
// 개발 환경: 포트 기반
s2Link = protocol + '//' + hostname + ':30180';
s3Link = protocol + '//' + hostname + ':30280';
}
document.getElementById('card-s2-link').href = s2Link;
document.getElementById('card-s3-link').href = s3Link;
}
setupSystemLinks();
init();
</script>
</body>

View File

@@ -1,38 +1,77 @@
/**
* 공유 네비게이션 헤더
* 공유 네비게이션 헤더 + SSO 쿠키 유틸리티
*
* 각 시스템 페이지에서 import하여 통합 포털 네비게이션을 제공
* 각 시스템 페이지에서 import하여 SSO 인증 유틸 제공
* <script src="/shared/nav-header.js"></script>
*/
(function() {
const TOKEN_KEY = 'sso_token';
const USER_KEY = 'sso_user';
// 쿠키 헬퍼
function cookieGet(name) {
var match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
return match ? decodeURIComponent(match[1]) : null;
}
function cookieSet(name, value, days) {
var cookie = name + '=' + encodeURIComponent(value) + '; path=/';
if (days) cookie += '; max-age=' + (days * 86400);
if (window.location.hostname.includes('technicalkorea.net')) {
cookie += '; domain=.technicalkorea.net; secure; samesite=lax';
}
document.cookie = cookie;
}
function cookieRemove(name) {
var cookie = name + '=; path=/; max-age=0';
if (window.location.hostname.includes('technicalkorea.net')) {
cookie += '; domain=.technicalkorea.net';
}
document.cookie = cookie;
}
/**
* SSO 토큰 가져오기
* SSO 인증 유틸리티 (쿠키 + localStorage 이중 지원)
*/
window.SSOAuth = {
getToken: function() {
return localStorage.getItem(TOKEN_KEY);
return cookieGet('sso_token') || localStorage.getItem('sso_token');
},
getUser: function() {
try {
return JSON.parse(localStorage.getItem(USER_KEY));
} catch {
return null;
}
var raw = cookieGet('sso_user') || localStorage.getItem('sso_user');
try { return JSON.parse(raw); } catch(e) { return null; }
},
isLoggedIn: function() {
return !!this.getToken();
var token = this.getToken();
return !!token && token !== 'undefined' && token !== 'null';
},
logout: function() {
localStorage.removeItem(TOKEN_KEY);
localStorage.removeItem(USER_KEY);
cookieRemove('sso_token');
cookieRemove('sso_user');
cookieRemove('sso_refresh_token');
localStorage.removeItem('sso_token');
localStorage.removeItem('sso_user');
localStorage.removeItem('sso_refresh_token');
window.location.href = '/login';
window.location.href = this.getLoginUrl();
},
/**
* 중앙 로그인 URL 반환
*/
getLoginUrl: function(redirect) {
var hostname = window.location.hostname;
var loginUrl;
if (hostname.includes('technicalkorea.net')) {
loginUrl = window.location.protocol + '//tkfb.technicalkorea.net/login';
} else {
// 개발 환경: Gateway 포트 (30000)
loginUrl = window.location.protocol + '//' + hostname + ':30000/login';
}
if (redirect) {
loginUrl += '?redirect=' + encodeURIComponent(redirect);
}
return loginUrl;
},
/**
@@ -41,7 +80,7 @@
fetch: function(url, options) {
options = options || {};
options.headers = options.headers || {};
const token = this.getToken();
var token = this.getToken();
if (token) {
options.headers['Authorization'] = 'Bearer ' + token;
}
@@ -49,27 +88,11 @@
},
/**
* 토큰 유효성 확인 (SSO 서비스 호출)
*/
validate: async function() {
const token = this.getToken();
if (!token) return false;
try {
const res = await fetch('/auth/validate', {
headers: { 'Authorization': 'Bearer ' + token }
});
return res.ok;
} catch {
return false;
}
},
/**
* 로그인 안 되어있으면 로그인 페이지로 리다이렉트
* 로그인 안 되어있으면 중앙 로그인 페이지로 리다이렉트
*/
requireLogin: function() {
if (!this.isLoggedIn()) {
window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname);
window.location.href = this.getLoginUrl(window.location.href);
}
}
};