package com.wb.domainserver.util; import com.wb.domainserver.realm.RealmContextHolder; import com.wb.domainserver.realm.util.UrlExtractor; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.util.Map; public abstract class CookieUtils { public static String getCookieValue(String cookieName, HttpServletRequest request){ String cookieValue = ""; if(request.getCookies() != null){ for(Cookie cookie : request.getCookies()){ if(cookieName.equals(cookie.getName())){ cookieValue = cookie.getValue(); break; } } } return cookieValue; } public static void deleteCookie(String name, String path, HttpServletRequest request, HttpServletResponse response) { String host = request.getHeader("X-Forwarded-Host"); if (host == null) { host = request.getServerName(); // 기본값으로 현재 요청의 호스트명 사용 } String originHost = UrlExtractor.getOriginalHost(host); Cookie cookie = new Cookie(name, null); cookie.setMaxAge(0); cookie.setPath(path); setCookieDomain(cookie, request, originHost); cookie.setHttpOnly(true); response.addCookie(cookie); // loc 환경에서는 이전에 상위 도메인으로 저장된 쿠키도 함께 삭제한다. deleteLegacyDomainCookie(name, path, request, response, originHost); } public static void createCookie(String name, String value, String path, int maxAge, boolean httpOnly, HttpServletRequest request, HttpServletResponse response) { String host = request.getHeader("X-Forwarded-Host"); if (host == null) { host = request.getServerName(); // 기본값으로 현재 요청의 호스트명 사용 } String originHost = UrlExtractor.getOriginalHost(host); deleteLegacyDomainCookie(name, path, request, response, originHost); Cookie cookie = new Cookie(name, value); cookie.setMaxAge(maxAge); cookie.setPath(path); setCookieDomain(cookie, request, originHost); cookie.setHttpOnly(httpOnly); response.addCookie(cookie); } public static void createCookie(String name, String value, String path, boolean httpOnly, HttpServletRequest request, HttpServletResponse response) { String host = request.getHeader("X-Forwarded-Host"); if (host == null) { host = request.getServerName(); // 기본값으로 현재 요청의 호스트명 사용 } String originHost = UrlExtractor.getOriginalHost(host); Cookie cookie = new Cookie(name, value); cookie.setPath(path); setCookieDomain(cookie, request, originHost); cookie.setHttpOnly(httpOnly); response.addCookie(cookie); } /** * 로그인 쿠키 경로 * */ public static Map loginCookiePath(){ String realm = RealmContextHolder.getRealm().toLowerCase(); // String ACTPath = "sopmal".equals(realm) ? "/" : "/" + realm; String ACTPath = "/"; String RFTPath = "/" + realm + "/tokens/refresh"; return Map.of("ACTPath", ACTPath, "RFTPath", RFTPath); } private static void setCookieDomain(Cookie cookie, HttpServletRequest request, String domain) { if (!request.getServerName().startsWith("loc.")) { cookie.setDomain(domain); } } private static void deleteLegacyDomainCookie(String name, String path, HttpServletRequest request, HttpServletResponse response, String domain) { if (request.getServerName().startsWith("loc.")) { addDomainDeleteCookie(name, path, domain, response); int firstDot = domain.indexOf('.'); if (firstDot > 0) { addDomainDeleteCookie(name, path, domain.substring(firstDot + 1), response); } } } private static void addDomainDeleteCookie(String name, String path, String domain, HttpServletResponse response) { Cookie domainCookie = new Cookie(name, null); domainCookie.setMaxAge(0); domainCookie.setPath(path); domainCookie.setDomain(domain); domainCookie.setHttpOnly(true); response.addCookie(domainCookie); } }