Package com.alpaca.security.manager
Class CookieManager
java.lang.Object
com.alpaca.security.manager.CookieManager
Utility class for managing HTTP cookies and serializing/deserializing objects in a secure manner.
Supports operations to read, add, delete cookies, as well as to serialize Java objects into
Base64-encoded JSON strings and to deserialize them back into objects. Custom Jackson
deserializers are registered to handle OAuth2-specific types like AuthRequestDeserializer
.
- Since:
- 1.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final com.fasterxml.jackson.databind.module.SimpleModule
private static final com.fasterxml.jackson.databind.ObjectMapper
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Adds a new cookie with specified attributes to the HTTP response.static void
deleteCookie
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String name) Deletes a cookie by name by setting its maximum age to zero and adding it to the response.static <T> T
deserialize
(jakarta.servlet.http.Cookie cookie, Class<T> t) Deserializes a cookie’s Base64-encoded JSON value into a Java object of the specified type.static Optional
<jakarta.servlet.http.Cookie> Retrieves a cookie by name from the HTTP request.static String
justSerialize
(Object object) Serializes a Java object to a standard JSON string without Base64 encoding.static String
Serializes a Java object into a Base64-encoded JSON string suitable for cookie storage.
-
Field Details
-
objectMapper
private static final com.fasterxml.jackson.databind.ObjectMapper objectMapper -
module
private static final com.fasterxml.jackson.databind.module.SimpleModule module
-
-
Constructor Details
-
CookieManager
public CookieManager()
-
-
Method Details
-
getCookie
public static Optional<jakarta.servlet.http.Cookie> getCookie(jakarta.servlet.http.HttpServletRequest request, String name) Retrieves a cookie by name from the HTTP request.- Parameters:
request
- the incomingHttpServletRequest
name
- the name of the cookie to retrieve- Returns:
- an
Optional
containing the cookie if found, otherwise empty
-
addCookie
public static void addCookie(jakarta.servlet.http.HttpServletResponse response, String name, String value, int maxAge) Adds a new cookie with specified attributes to the HTTP response.- Parameters:
response
- theHttpServletResponse
to which the cookie will be addedname
- the name of the cookievalue
- the value of the cookiemaxAge
- maximum age in seconds for the cookie
-
deleteCookie
public static void deleteCookie(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String name) Deletes a cookie by name by setting its maximum age to zero and adding it to the response.- Parameters:
request
- theHttpServletRequest
containing existing cookiesresponse
- theHttpServletResponse
to which the deletion command is sentname
- the name of the cookie to delete
-
serialize
Serializes a Java object into a Base64-encoded JSON string suitable for cookie storage.- Parameters:
object
- the object to serialize- Returns:
- a Base64 URL-safe encoded JSON string
- Throws:
RuntimeException
- if serialization fails
-
deserialize
Deserializes a cookie’s Base64-encoded JSON value into a Java object of the specified type.- Type Parameters:
T
- the generic type parameter- Parameters:
cookie
- the cookie containing the encoded valuet
- the target type to deserialize into- Returns:
- the deserialized object
- Throws:
RuntimeException
- if deserialization fails
-
justSerialize
Serializes a Java object to a standard JSON string without Base64 encoding.- Parameters:
object
- the object to serialize- Returns:
- the JSON string
- Throws:
RuntimeException
- if serialization fails
-