Class CookieManager
- All Implemented Interfaces:
Component,DestructableComponent,InitializableComponent
This bean centralizes settings related to cookie creation and access, and is parameterized by name so that multiple cookies may be managed with common properties.
Some of the features depend on Servlet API >= 6.0 and will be conditionally bypassed otherwise.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe allowed same-site cookie attribute values. -
Field Summary
FieldsModifier and TypeFieldDescriptionAdditional cookie attributes.private StringDomain of cookie.private intLimit on numbeer of cookies when purging.private StringPath of cookie.private CookieManager.SameSiteValueSameSite attribute.private booleanWhether to guardCookie.setAttribute(String, String)calls.private booleanIs cookie marked HttpOnly?private NonnullSupplier<HttpServletRequest>Supplier for the servlet request to read from.private NonnullSupplier<HttpServletResponse>Supplier for the servlet response to write to.private org.slf4j.LoggerClass logger.private intMaximum age in seconds, or -1 for session.private Predicate<HttpServletRequest>Condition controlling application of SameSite.private Map<String,CookieManager.SameSiteValue> Map of cookie name to same-site attribute value.private booleanIs cookie secure? -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a cookie with the specified attributes.voidAdd a cookie with the specified attributes.voidAdd a cookie with the specified attributes.private voidattachSameSite(Cookie cookie) Implementation of SameSite attachment logic when available.private StringTurn the servlet context path into an appropriate cookie path.booleancookieHasValue(String name, String expectedValue) Check whether a cookie has a certain value.protected voidPerforms the initialization of the component.Gets additional attributes to apply to the cookie.Get the cookie domain to use.intGets the limit on cookies of a given set when purging.Get the cookie path to use.getCookieValue(String name, String defValue) Return the first matching cookie's value.protected HttpServletRequestGet the current HTTP request if available.protected HttpServletResponseGet the current HTTP response if available.intGet maximum age of cookies in seconds, or -1 for a session cookie.Gets the SameSite attribute.Gets the condition controlling application of SameSite.booleanGet the HttpOnly flag.booleanisSecure()Get the TLS-only flag.voidpurgeStaleCookies(String prefix) Unset cookies matching a given prefix in excess of the configured amount.voidpurgeStaleCookies(String prefix, String overridePath) Unset cookies matching a given prefix in excess of the configured amount.voidsetCookieAttributes(Map<String, String> attributes) Sets additional attributes to apply to the cookie.voidsetCookieDomain(String domain) Set the cookie domain to use.voidsetCookieLimit(int limit) Sets the limit on cookies of a given set when purging.voidsetCookiePath(String path) Set the cookie path to use.voidsetGuardSetAttribute(boolean flag) Sets whether to guard calls toCookie.setAttribute(String, String)with a check for the container's servlet API version.voidsetHttpOnly(boolean flag) Set the HttpOnly flag.voidsetHttpServletRequestSupplier(NonnullSupplier<HttpServletRequest> requestSupplier) Set the Supplier for the servlet request to read from.voidsetHttpServletResponseSupplier(NonnullSupplier<HttpServletResponse> responseSupplier) Set the supplier for the servlet response to write to.voidsetMaxAge(int age) Maximum age in seconds, or -1 for a session cookie.voidMaximum age expressed as aDuration.voidSets the SameSite attribute.voidsetSameSiteCondition(Predicate<HttpServletRequest> condition) Sets the condition controlling application of SameSite.voidSet the names of cookies to add the same-site attribute to.voidsetSecure(boolean flag) Set the TLS-only flag.voidunsetCookie(String name) Unsets a cookie with the specified name and the default path.voidunsetCookie(String name, String overridePath) Unsets a cookie with the specified name and path.Methods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent
checkComponentActive, checkSetterPreconditions, destroy, doDestroy, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentException, initialize, isDestroyed, isInitialized
-
Field Details
-
log
@Nonnull private org.slf4j.Logger logClass logger. -
cookiePath
Path of cookie. -
cookieDomain
Domain of cookie. -
httpRequestSupplier
Supplier for the servlet request to read from. -
httpResponseSupplier
Supplier for the servlet response to write to. -
secure
private boolean secureIs cookie secure? -
httpOnly
private boolean httpOnlyIs cookie marked HttpOnly? -
maxAge
private int maxAgeMaximum age in seconds, or -1 for session. -
guardSetAttribute
private boolean guardSetAttributeWhether to guardCookie.setAttribute(String, String)calls. -
defaultSameSite
SameSite attribute. -
sameSiteCookies
Map of cookie name to same-site attribute value. -
sameSiteCondition
Condition controlling application of SameSite. -
cookieAttributes
Additional cookie attributes. -
cookieLimit
Limit on numbeer of cookies when purging.
-
-
Constructor Details
-
CookieManager
public CookieManager()Constructor.
-
-
Method Details
-
getCookiePath
Get the cookie path to use.- Returns:
- cookie path
- Since:
- 9.1.0
-
setCookiePath
Set the cookie path to use.Defaults to the servlet context path.
- Parameters:
path- cookie path to use, or null for the default
-
getCookieDomain
Get the cookie domain to use.- Returns:
- cookie domain
- Since:
- 9.1.0
-
setCookieDomain
Set the cookie domain to use.- Parameters:
domain- the cookie domain to use, or null for the default
-
getHttpServletRequest
Get the current HTTP request if available.- Returns:
- current HTTP request
-
getHttpServletResponse
Get the current HTTP response if available.- Returns:
- current HTTP response or null
-
isSecure
public boolean isSecure()Get the TLS-only flag.- Returns:
- TLS-only flag
- Since:
- 9.2.0
-
setSecure
public void setSecure(boolean flag) Set the TLS-only flag.- Parameters:
flag- flag to set
-
isHttpOnly
public boolean isHttpOnly()Get the HttpOnly flag.- Returns:
- HttpOnly flag
- Since:
- 9.2.0
-
setHttpOnly
public void setHttpOnly(boolean flag) Set the HttpOnly flag.- Parameters:
flag- flag to set
-
getMaxAge
public int getMaxAge()Get maximum age of cookies in seconds, or -1 for a session cookie.Note that this "convention" for -1 is NOT consistent with the RFC and is a hold-over from Java's native Cookie class, probably because the Integer type didn't exist, so there was no way o easily handle a null Max-Age value in the API.
- Returns:
- max age in seconds, or -1 for a session cookie
- Since:
- 9.1.0
-
setMaxAge
public void setMaxAge(int age) Maximum age in seconds, or -1 for a session cookie.Note that this "convention" for -1 is NOT consistent with the RFC and is a hold-over from Java's native Cookie class, probably because the Integer type didn't exist, so there was no way o easily handle a null Max-Age value in the API.
- Parameters:
age- max age to set
-
setMaxAgeDuration
Maximum age expressed as aDuration.- Parameters:
age- max age of cookie- Since:
- 9.0.0
-
setGuardSetAttribute
public void setGuardSetAttribute(boolean flag) Sets whether to guard calls toCookie.setAttribute(String, String)with a check for the container's servlet API version.Defaults to true.
- Parameters:
flag-
-
getSameSite
Gets the SameSite attribute.- Returns:
- the SameSite attribute
- Since:
- 9.2.0
-
setSameSiteCookies
Set the names of cookies to add the same-site attribute to.The argument map is flattened to remove the nested collection. The argument map allows duplicate cookie names to appear in order to detect configuration errors which would otherwise not be found during argument injection e.g. trying to set a session identifier cookie as both SameSite=Strict and SameSite=None. Instead, duplicates are detected here, throwing a terminating
IllegalArgumentExceptionif found.- Parameters:
map- the map of same-site attribute values to cookie names- Since:
- 9.2.0
-
getSameSiteCondition
Gets the condition controlling application of SameSite.- Returns:
- condition
- Since:
- 9.2.0
-
setSameSiteCondition
Sets the condition controlling application of SameSite.Defaults to true.
- Parameters:
condition- condition to set- Since:
- 9.2.0
-
getCookieAttributes
Gets additional attributes to apply to the cookie.- Returns:
- additional attributes
- Since:
- 9.2.0
-
setCookieAttributes
Sets additional attributes to apply to the cookie.- Parameters:
attributes- attributes to apply- Since:
- 9.2.0
-
getCookieLimit
Gets the limit on cookies of a given set when purging.- Returns:
- the limit or 0 for unlimited
- Since:
- 9.2.0
-
setCookieLimit
Sets the limit on cookies of a given set when purging.Defaults to 0, no limit.
- Parameters:
limit- limit to set or 0 for unlimited- Since:
- 9.2.0
-
doInitialize
Performs the initialization of the component. This method is executed within the lock on the object being initialized. The default implementation of this method is a no-op.- Overrides:
doInitializein classAbstractInitializableComponent- Throws:
ComponentInitializationException- thrown if there is a problem initializing the component
-
addCookie
Add a cookie with the specified attributes.- Parameters:
name- name of cookievalue- value of cookie
-
addCookie
public void addCookie(@Nonnull @NotEmpty String name, @Nonnull @NotEmpty String value, int overrideMaxAge) Add a cookie with the specified attributes.- Parameters:
name- name of cookievalue- value of cookieoverrideMaxAge- max-age value to use- Since:
- 9.1.0
-
addCookie
public void addCookie(@Nonnull @NotEmpty String name, @Nonnull @NotEmpty String value, @Nullable @NotEmpty String overridePath, int overrideMaxAge) Add a cookie with the specified attributes.- Parameters:
name- name of cookievalue- value of cookieoverridePath- path value tp useoverrideMaxAge- max-age value to use- Since:
- 9.2.0
-
unsetCookie
Unsets a cookie with the specified name and the default path.- Parameters:
name- name of cookie
-
unsetCookie
Unsets a cookie with the specified name and path.- Parameters:
name- name of cookieoverridePath- cookie path
-
cookieHasValue
public boolean cookieHasValue(@Nonnull @NotEmpty String name, @Nonnull @NotEmpty String expectedValue) Check whether a cookie has a certain value.- Parameters:
name- name of cookieexpectedValue- expected value of cookie- Returns:
- true iff the cookie exists and has the expected value
-
getCookieValue
Return the first matching cookie's value.- Parameters:
name- cookie namedefValue- default value to return if the cookie isn't found- Returns:
- cookie value
-
purgeStaleCookies
Unset cookies matching a given prefix in excess of the configured amount.- Parameters:
prefix- cookie name prefix to match on- Since:
- 9.2.0
-
purgeStaleCookies
public void purgeStaleCookies(@Nonnull @NotEmpty String prefix, @Nullable @NotEmpty String overridePath) Unset cookies matching a given prefix in excess of the configured amount.- Parameters:
prefix- cookie name prefix to match onoverridePath- cookie path- Since:
- 9.2.0
-
attachSameSite
Implementation of SameSite attachment logic when available.- Parameters:
cookie- cookie to attach attribute to
-
contextPathToCookiePath
Turn the servlet context path into an appropriate cookie path.- Returns:
- the cookie path
-