Files
sake/src/main/java/com/wb/domainserver/util/CookieUtils.java
2025-11-12 11:31:25 +09:00

100 lines
3.0 KiB
Java

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);
cookie.setDomain(originHost);
cookie.setHttpOnly(true);
response.addCookie(cookie);
}
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);
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(maxAge);
cookie.setPath(path);
cookie.setDomain(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);
cookie.setDomain(originHost);
cookie.setHttpOnly(httpOnly);
response.addCookie(cookie);
}
/**
* 로그인 쿠키 경로
* */
public static Map<String, String> 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);
}
}