-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from reading-log/develop
쿠키에 토큰 값 저장 테스트
- Loading branch information
Showing
4 changed files
with
73 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/api/readinglog/common/security/util/CookieUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.api.readinglog.common.security.util; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.Base64; | ||
import java.util.Optional; | ||
import org.springframework.util.SerializationUtils; | ||
|
||
public class CookieUtils { | ||
|
||
public static Optional<Cookie> getCookie(HttpServletRequest request, String name) { | ||
Cookie[] cookies = request.getCookies(); | ||
|
||
if (cookies != null && cookies.length > 0) { | ||
for (Cookie cookie : cookies) { | ||
if (name.equals(cookie.getName())) { | ||
return Optional.of(cookie); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) { | ||
Cookie cookie = new Cookie(name, value); | ||
cookie.setPath("/"); | ||
cookie.setHttpOnly(true); | ||
cookie.setMaxAge(maxAge); | ||
|
||
response.addCookie(cookie); | ||
} | ||
|
||
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String name) { | ||
Cookie[] cookies = request.getCookies(); | ||
|
||
if (cookies != null && cookies.length > 0) { | ||
for (Cookie cookie : cookies) { | ||
if (name.equals(cookie.getName())) { | ||
cookie.setValue(""); | ||
cookie.setPath("/"); | ||
cookie.setMaxAge(0); | ||
response.addCookie(cookie); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static String serialize(Object obj) { | ||
return Base64.getUrlEncoder() | ||
.encodeToString(SerializationUtils.serialize(obj)); | ||
} | ||
|
||
public static <T> T deserialize(Cookie cookie, Class<T> cls) { | ||
return cls.cast( | ||
SerializationUtils.deserialize( | ||
Base64.getUrlDecoder().decode(cookie.getValue()) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters