Skip to content

Commit 8bbb58e

Browse files
authored
Merge pull request #27 from Decodeat/refactor/22-change-redirect-url
feat: CoolkieUtil 수정
2 parents ec69506 + 742f154 commit 8bbb58e

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/main/java/com/DecodEat/global/util/CookieUtil.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import jakarta.servlet.http.Cookie;
44
import jakarta.servlet.http.HttpServletRequest;
55
import jakarta.servlet.http.HttpServletResponse;
6+
import org.springframework.http.ResponseCookie;
67
import org.springframework.util.SerializationUtils;
78

89
import java.io.*;
@@ -24,15 +25,15 @@ public static Optional<Cookie> getCookie(HttpServletRequest request, String name
2425
}
2526

2627
// 응답 객체(response)에 쿠키를 추가하는 메소드
27-
// httpOnly: true -> 자바스크립트에서 쿠키에 접근 불가
28-
// secure: true -> HTTPS 통신에서만 쿠_cookie 전송
2928
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
30-
Cookie cookie = new Cookie(name, value);
31-
cookie.setPath("/"); // 쿠키가 적용될 경로
32-
cookie.setMaxAge(maxAge); // 쿠키의 유효 기간(초 단위)
33-
cookie.setHttpOnly(true); // JavaScript를 통한 접근 방지
34-
// cookie.setSecure(true); // HTTPS를 사용하는 경우에만 활성화
35-
response.addCookie(cookie);
29+
ResponseCookie cookie = ResponseCookie.from(name, value)
30+
.path("/")
31+
.maxAge(maxAge)
32+
.httpOnly(true)
33+
.secure(true)
34+
.sameSite("None")
35+
.build();
36+
response.addHeader("Set-Cookie", cookie.toString());
3637
}
3738

3839
// 특정 이름의 쿠키를 삭제하는 메소드
@@ -41,10 +42,13 @@ public static void deleteCookie(HttpServletRequest request, HttpServletResponse
4142
if (cookies != null && cookies.length > 0) {
4243
for (Cookie cookie : cookies) {
4344
if (name.equals(cookie.getName())) {
44-
cookie.setValue("");
45-
cookie.setPath("/");
46-
cookie.setMaxAge(0); // 유효 기간을 0으로 설정하여 즉시 만료
47-
response.addCookie(cookie);
45+
ResponseCookie deleteCookie = ResponseCookie.from(name, "")
46+
.path("/")
47+
.maxAge(0)
48+
.secure(true)
49+
.sameSite("None")
50+
.build();
51+
response.addHeader("Set-Cookie", deleteCookie.toString());
4852
}
4953
}
5054
}
@@ -61,4 +65,4 @@ public static <T> T deserialize(Cookie cookie, Class<T> cls) {
6165
byte[] decodedBytes = Base64.getUrlDecoder().decode(cookie.getValue());
6266
return cls.cast(SerializationUtils.deserialize(decodedBytes));
6367
}
64-
}
68+
}

0 commit comments

Comments
 (0)