Skip to content

Commit

Permalink
fix :: cookie 설정 추가 - https
Browse files Browse the repository at this point in the history
  • Loading branch information
hyundong-L committed Nov 28, 2024
1 parent 3cfd305 commit 041ec64
Showing 1 changed file with 2 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CookieUtil {
private static final int MAX_LIST_SIZE = 5;
Expand All @@ -21,8 +18,9 @@ public static void addCookie(HttpServletResponse response, String cookieName, St
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setPath("/");
cookie.setMaxAge(expireSeconds);
// cookie.setSecure(true); // 보안 쿠키로 설정 (HTTPS에서만 사용)
cookie.setSecure(true);
cookie.setHttpOnly(true);
cookie.setDomain(".youthmap.site");
response.addCookie(cookie);
}

Expand All @@ -36,39 +34,6 @@ public static void deleteCookie(HttpServletRequest request, HttpServletResponse
}
}

public static void setLongListCookie(HttpServletResponse response, String cookieName, List<Long> longList, int expireSeconds) {
if (longList.size() > MAX_LIST_SIZE) {
longList.remove(0);
}

String cookieValue = longList.stream()
.map(String::valueOf)
.collect(Collectors.joining(","));

Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setMaxAge(expireSeconds);
cookie.setPath("/");

response.addCookie(cookie);
}

public static List<Long> getLongListFromCookie(HttpServletRequest request, String cookieName) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(cookieName)) {
String cookieValue = cookie.getValue();

return Arrays.stream(cookieValue.split(","))
.map(Long::valueOf)
.collect(Collectors.toList());
}
}
}
return new ArrayList<>();
}


private static Cookie getCookie(HttpServletRequest request, String cookieName) {
if (request.getCookies() != null) {
return Arrays.stream(request.getCookies())
Expand Down

0 comments on commit 041ec64

Please sign in to comment.