Top Description Inners Fields Constructors Methods
org.springframework.http

public final Class ResponseCookie

extends HttpCookie
Class Inheritance
Imports
java.time.Duration, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils, .StringUtils

An HttpCookie subclass with the additional attributes allowed in the "Set-Cookie" response header. To build an instance use the from static method.
Authors
Rossen Stoyanchev, Brian Clozel
Since
5.0
See Also
RFC 6265

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static interface
ResponseCookie.ResponseCookieBuilder

A builder for a server-defined HttpCookie with attributes.

private static class

Field Summary

Modifier and TypeField and Description
private final String
private final boolean
private final Duration
private final String
private final String
private final boolean

Constructor Summary

AccessConstructor and Description
private
ResponseCookie(String name, String value, Duration maxAge, String domain, String path, boolean secure, boolean httpOnly, String sameSite)

Private constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.http.HttpCookie.equals.

Indicates whether some other object is "equal to" this one.
public static ResponseCookie.ResponseCookieBuilder

Returns:

a builder to create the cookie with
from
(final String
the cookie name
name
)

Factory method to obtain a builder for a server-defined cookie, given its name only, and where the value as well as other attributes can be set later via builder methods.

public static ResponseCookie.ResponseCookieBuilder

Returns:

a builder to create the cookie with
from
(final String
the cookie name
name
,
final String
the cookie value
value
)

Factory method to obtain a builder for a server-defined cookie that starts with a name-value pair and may also include attributes.

public static ResponseCookie.ResponseCookieBuilder

Returns:

a builder to create the cookie with
fromClientResponse
(final String
the cookie name
name
,
final String
the cookie value
value
)

Factory method to obtain a builder for a server-defined cookie.

public String
getDomain()

Return the cookie "Domain" attribute, or null if not set.

public Duration
getMaxAge()

Return the cookie "Max-Age" attribute in seconds.

public String
getPath()

Return the cookie "Path" attribute, or null if not set.

public String
getSameSite()

Return the cookie "SameSite" attribute, or null if not set.

public int
hashCode()

Overrides org.springframework.http.HttpCookie.hashCode.

Returns a hash code value for the object.
public boolean
isHttpOnly()

Return true if the cookie has the "HttpOnly" attribute.

public boolean
isSecure()

Return true if the cookie has the "Secure" attribute.

public ResponseCookie.ResponseCookieBuilder
mutate()

Return a builder pre-populated with values from "this" instance.

public String
toString()

Overrides org.springframework.http.HttpCookie.toString.

Returns a string representation of the object.
Inherited from org.springframework.http.HttpCookie:
getNamegetValue

Field Detail

domainback to summary
private final String domain
Annotations
@Nullable
httpOnlyback to summary
private final boolean httpOnly
maxAgeback to summary
private final Duration maxAge
pathback to summary
private final String path
Annotations
@Nullable
sameSiteback to summary
private final String sameSite
Annotations
@Nullable
secureback to summary
private final boolean secure

Constructor Detail

ResponseCookieback to summary
private ResponseCookie(String name, String value, Duration maxAge, String domain, String path, boolean secure, boolean httpOnly, String sameSite)

Private constructor. See from(String, String).

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides org.springframework.http.HttpCookie.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
fromback to summary
public static ResponseCookie.ResponseCookieBuilder from(final String name)

Factory method to obtain a builder for a server-defined cookie, given its name only, and where the value as well as other attributes can be set later via builder methods.

Parameters
name:String

the cookie name

Returns:ResponseCookie.ResponseCookieBuilder

a builder to create the cookie with

Since
6.0
fromback to summary
public static ResponseCookie.ResponseCookieBuilder from(final String name, final String value)

Factory method to obtain a builder for a server-defined cookie that starts with a name-value pair and may also include attributes.

Parameters
name:String

the cookie name

value:String

the cookie value

Returns:ResponseCookie.ResponseCookieBuilder

a builder to create the cookie with

fromClientResponseback to summary
public static ResponseCookie.ResponseCookieBuilder fromClientResponse(final String name, final String value)

Factory method to obtain a builder for a server-defined cookie. Unlike from(String, String) this option assumes input from a remote server, which can be handled more leniently, e.g. ignoring an empty domain name with double quotes.

Parameters
name:String

the cookie name

value:String

the cookie value

Returns:ResponseCookie.ResponseCookieBuilder

a builder to create the cookie with

Since
5.2.5
getDomainback to summary
public String getDomain()

Return the cookie "Domain" attribute, or null if not set.

Annotations
@Nullable
getMaxAgeback to summary
public Duration getMaxAge()

Return the cookie "Max-Age" attribute in seconds.

A positive value indicates when the cookie expires relative to the current time. A value of 0 means the cookie should expire immediately. A negative value means no "Max-Age" attribute in which case the cookie is removed when the browser is closed.

getPathback to summary
public String getPath()

Return the cookie "Path" attribute, or null if not set.

Annotations
@Nullable
getSameSiteback to summary
public String getSameSite()

Return the cookie "SameSite" attribute, or null if not set.

This limits the scope of the cookie such that it will only be attached to same site requests if "Strict" or cross-site requests if "Lax".

Annotations
@Nullable
Since
5.1
See Also
RFC6265 bis
hashCodeback to summary
public int hashCode()

Overrides org.springframework.http.HttpCookie.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isHttpOnlyback to summary
public boolean isHttpOnly()

Return true if the cookie has the "HttpOnly" attribute.

See Also
https://www.owasp.org/index.php/HTTPOnly
isSecureback to summary
public boolean isSecure()

Return true if the cookie has the "Secure" attribute.

mutateback to summary
public ResponseCookie.ResponseCookieBuilder mutate()

Return a builder pre-populated with values from "this" instance.

Since
6.0
toStringback to summary
public String toString()

Overrides org.springframework.http.HttpCookie.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.springframework.http back to summary

private Class ResponseCookie.DefaultResponseCookieBuilder

extends Object
implements ResponseCookieBuilder
Class Inheritance
  • java.lang.Object
  • org.springframework.http.ResponseCookie.DefaultResponseCookieBuilder
All Implemented Interfaces
org.springframework.http.ResponseCookie.ResponseCookieBuilder

Default implementation of ResponseCookieBuilder.

Field Summary

Modifier and TypeField and Description
private String
private boolean
private final boolean
private Duration
private final String
private String
private String
private boolean
private String

Constructor Summary

AccessConstructor and Description
public
DefaultResponseCookieBuilder(String name, String value, boolean lenient)

Method Summary

Modifier and TypeMethod and Description
public ResponseCookie
public ResponseCookie.ResponseCookieBuilder
domain(String domain)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.domain.

Set the cookie "Domain" attribute.
public ResponseCookie.ResponseCookieBuilder
httpOnly(boolean httpOnly)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.httpOnly.

Add the "HttpOnly" attribute to the cookie.
private String
public ResponseCookie.ResponseCookieBuilder
maxAge(Duration maxAge)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.maxAge.

Set the cookie "Max-Age" attribute.
public ResponseCookie.ResponseCookieBuilder
maxAge(long maxAgeSeconds)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.maxAge.

Variant of maxAge(Duration) accepting a value in seconds.
public ResponseCookie.ResponseCookieBuilder
path(String path)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.path.

Set the cookie "Path" attribute.
public ResponseCookie.ResponseCookieBuilder
sameSite(String sameSite)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.sameSite.

Add the "SameSite" attribute to the cookie.
public ResponseCookie.ResponseCookieBuilder
secure(boolean secure)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.secure.

Add the "Secure" attribute to the cookie.
public ResponseCookie.ResponseCookieBuilder
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

domainback to summary
private String domain
Annotations
@Nullable
httpOnlyback to summary
private boolean httpOnly
lenientback to summary
private final boolean lenient
maxAgeback to summary
private Duration maxAge
nameback to summary
private final String name
pathback to summary
private String path
Annotations
@Nullable
sameSiteback to summary
private String sameSite
Annotations
@Nullable
secureback to summary
private boolean secure
valueback to summary
private String value
Annotations
@Nullable

Constructor Detail

DefaultResponseCookieBuilderback to summary
public DefaultResponseCookieBuilder(String name, String value, boolean lenient)

Method Detail

buildback to summary
public ResponseCookie build()

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.build.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.build.

Create the HttpCookie.

Annotations
@Override
domainback to summary
public ResponseCookie.ResponseCookieBuilder domain(String domain)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.domain.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.domain.

Set the cookie "Domain" attribute.

Annotations
@Override
httpOnlyback to summary
public ResponseCookie.ResponseCookieBuilder httpOnly(boolean httpOnly)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.httpOnly.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.httpOnly.

Add the "HttpOnly" attribute to the cookie.

Annotations
@Override
initDomainback to summary
private String initDomain(String domain)
Annotations
@Nullable
maxAgeback to summary
public ResponseCookie.ResponseCookieBuilder maxAge(Duration maxAge)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.maxAge.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.maxAge.

Set the cookie "Max-Age" attribute.

A positive value indicates when the cookie should expire relative to the current time. A value of 0 means the cookie should expire immediately. A negative value results in no "Max-Age" attribute in which case the cookie is removed when the browser is closed.

Annotations
@Override
maxAgeback to summary
public ResponseCookie.ResponseCookieBuilder maxAge(long maxAgeSeconds)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.maxAge.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.maxAge.

Variant of maxAge(Duration) accepting a value in seconds.

Annotations
@Override
pathback to summary
public ResponseCookie.ResponseCookieBuilder path(String path)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.path.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.path.

Set the cookie "Path" attribute.

Annotations
@Override
sameSiteback to summary
public ResponseCookie.ResponseCookieBuilder sameSite(String sameSite)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.sameSite.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.sameSite.

Add the "SameSite" attribute to the cookie.

This limits the scope of the cookie such that it will only be attached to same site requests if "Strict" or cross-site requests if "Lax".

Annotations
@Override
secureback to summary
public ResponseCookie.ResponseCookieBuilder secure(boolean secure)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.secure.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.secure.

Add the "Secure" attribute to the cookie.

Annotations
@Override
valueback to summary
public ResponseCookie.ResponseCookieBuilder value(String value)

Implements org.springframework.http.ResponseCookie.ResponseCookieBuilder.value.

Doc from org.springframework.http.ResponseCookie.ResponseCookieBuilder.value.

Set the cookie value.

Annotations
@Override
org.springframework.http back to summary

public Interface ResponseCookie.ResponseCookieBuilder

Known Direct Implementers
org.springframework.http.ResponseCookie.DefaultResponseCookieBuilder

A builder for a server-defined HttpCookie with attributes.

Method Summary

Modifier and TypeMethod and Description
public ResponseCookie
build()

Create the HttpCookie.

public ResponseCookie.ResponseCookieBuilder
domain(String domain)

Set the cookie "Domain" attribute.

public ResponseCookie.ResponseCookieBuilder
httpOnly(boolean httpOnly)

Add the "HttpOnly" attribute to the cookie.

public ResponseCookie.ResponseCookieBuilder
maxAge(Duration maxAge)

Set the cookie "Max-Age" attribute.

public ResponseCookie.ResponseCookieBuilder
maxAge(long maxAgeSeconds)

Variant of maxAge(Duration) accepting a value in seconds.

public ResponseCookie.ResponseCookieBuilder
path(String path)

Set the cookie "Path" attribute.

public ResponseCookie.ResponseCookieBuilder
sameSite(String sameSite)

Add the "SameSite" attribute to the cookie.

public ResponseCookie.ResponseCookieBuilder
secure(boolean secure)

Add the "Secure" attribute to the cookie.

public ResponseCookie.ResponseCookieBuilder
value(String value)

Set the cookie value.

Method Detail

buildback to summary
public ResponseCookie build()

Create the HttpCookie.

domainback to summary
public ResponseCookie.ResponseCookieBuilder domain(String domain)

Set the cookie "Domain" attribute.

httpOnlyback to summary
public ResponseCookie.ResponseCookieBuilder httpOnly(boolean httpOnly)

Add the "HttpOnly" attribute to the cookie.

See Also
https://www.owasp.org/index.php/HTTPOnly
maxAgeback to summary
public ResponseCookie.ResponseCookieBuilder maxAge(Duration maxAge)

Set the cookie "Max-Age" attribute.

A positive value indicates when the cookie should expire relative to the current time. A value of 0 means the cookie should expire immediately. A negative value results in no "Max-Age" attribute in which case the cookie is removed when the browser is closed.

maxAgeback to summary
public ResponseCookie.ResponseCookieBuilder maxAge(long maxAgeSeconds)

Variant of maxAge(Duration) accepting a value in seconds.

pathback to summary
public ResponseCookie.ResponseCookieBuilder path(String path)

Set the cookie "Path" attribute.

sameSiteback to summary
public ResponseCookie.ResponseCookieBuilder sameSite(String sameSite)

Add the "SameSite" attribute to the cookie.

This limits the scope of the cookie such that it will only be attached to same site requests if "Strict" or cross-site requests if "Lax".

Since
5.1
See Also
RFC6265 bis
secureback to summary
public ResponseCookie.ResponseCookieBuilder secure(boolean secure)

Add the "Secure" attribute to the cookie.

valueback to summary
public ResponseCookie.ResponseCookieBuilder value(String value)

Set the cookie value.

Since
6.0
org.springframework.http back to summary

private Class ResponseCookie.Rfc6265Utils

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
public static void
public static void
public static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DOMAIN_CHARSback to summary
private static final String DOMAIN_CHARS
SEPARATOR_CHARSback to summary
private static final String SEPARATOR_CHARS

Constructor Detail

Rfc6265Utilsback to summary
private Rfc6265Utils()

Method Detail

validateCookieNameback to summary
public static void validateCookieName(String name)
validateCookieValueback to summary
public static void validateCookieValue(String value)
validateDomainback to summary
public static void validateDomain(String domain)
validatePathback to summary
public static void validatePath(String path)