Class CookieCsrfTokenRepository
java.lang.Object
org.springframework.security.web.csrf.CookieCsrfTokenRepository
- All Implemented Interfaces:
- CsrfTokenRepository
A 
CsrfTokenRepository that persists the CSRF token in a cookie named
 "XSRF-TOKEN" and reads from the header "X-XSRF-TOKEN" following the conventions of
 AngularJS. When using with AngularJS be sure to use withHttpOnlyFalse().- Since:
- 4.1
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptiongenerateToken(jakarta.servlet.http.HttpServletRequest request) Generates aCsrfTokenGet the path that the CSRF cookie will be set to.loadToken(jakarta.servlet.http.HttpServletRequest request) Loads the expectedCsrfTokenfrom theHttpServletRequestvoidsaveToken(CsrfToken token, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) voidsetCookieCustomizer(Consumer<org.springframework.http.ResponseCookie.ResponseCookieBuilder> cookieCustomizer) Add aConsumerfor aResponseCookieBuilderthat will be invoked for each cookie being built, just before the call tobuild().voidsetCookieDomain(String cookieDomain) Deprecated.voidsetCookieHttpOnly(boolean cookieHttpOnly) Deprecated.UsesetCookieCustomizer(Consumer)instead.voidsetCookieMaxAge(int cookieMaxAge) Deprecated.UsesetCookieCustomizer(Consumer)instead.voidsetCookieName(String cookieName) Sets the name of the cookie that the expected CSRF token is saved to and read from.voidsetCookiePath(String path) Set the path that the Cookie will be created with.voidsetHeaderName(String headerName) Sets the name of the HTTP header that should be used to provide the token.voidsetParameterName(String parameterName) Sets the name of the HTTP request parameter that should be used to provide a token.voidDeprecated.UsesetCookieCustomizer(Consumer)instead.static CookieCsrfTokenRepositoryFactory method to conveniently create an instance that creates cookies whereCookie.isHttpOnly()is set to false.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.security.web.csrf.CsrfTokenRepositoryloadDeferredToken
- 
Constructor Details- 
CookieCsrfTokenRepositorypublic CookieCsrfTokenRepository()
 
- 
- 
Method Details- 
setCookieCustomizerpublic void setCookieCustomizer(Consumer<org.springframework.http.ResponseCookie.ResponseCookieBuilder> cookieCustomizer) Add aConsumerfor aResponseCookieBuilderthat will be invoked for each cookie being built, just before the call tobuild().- Parameters:
- cookieCustomizer- consumer for a cookie builder
- Since:
- 6.1
 
- 
generateTokenDescription copied from interface:CsrfTokenRepositoryGenerates aCsrfToken- Specified by:
- generateTokenin interface- CsrfTokenRepository
- Parameters:
- request- the- HttpServletRequestto use
- Returns:
- the CsrfTokenthat was generated. Cannot be null.
 
- 
saveTokenpublic void saveToken(CsrfToken token, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Description copied from interface:CsrfTokenRepositorySaves theCsrfTokenusing theHttpServletRequestandHttpServletResponse. If theCsrfTokenis null, it is the same as deleting it.- Specified by:
- saveTokenin interface- CsrfTokenRepository
- Parameters:
- token- the- CsrfTokento save or null to delete
- request- the- HttpServletRequestto use
- response- the- HttpServletResponseto use
 
- 
loadTokenDescription copied from interface:CsrfTokenRepositoryLoads the expectedCsrfTokenfrom theHttpServletRequest- Specified by:
- loadTokenin interface- CsrfTokenRepository
- Parameters:
- request- the- HttpServletRequestto use
- Returns:
- the CsrfTokenor null if none exists
 
- 
setParameterNameSets the name of the HTTP request parameter that should be used to provide a token.- Parameters:
- parameterName- the name of the HTTP request parameter that should be used to provide a token
 
- 
setHeaderNameSets the name of the HTTP header that should be used to provide the token.- Parameters:
- headerName- the name of the HTTP header that should be used to provide the token
 
- 
setCookieNameSets the name of the cookie that the expected CSRF token is saved to and read from.- Parameters:
- cookieName- the name of the cookie that the expected CSRF token is saved to and read from
 
- 
setCookieHttpOnlyDeprecated.UsesetCookieCustomizer(Consumer)instead.
- 
withHttpOnlyFalseFactory method to conveniently create an instance that creates cookies whereCookie.isHttpOnly()is set to false.- Returns:
- an instance of CookieCsrfTokenRepository that creates cookies where
 Cookie.isHttpOnly()is set to false.
 
- 
setCookiePathSet the path that the Cookie will be created with. This will override the default functionality which uses the request context as the path.- Parameters:
- path- the path to use
 
- 
getCookiePathGet the path that the CSRF cookie will be set to.- Returns:
- the path to be used.
 
- 
setCookieDomainDeprecated.UsesetCookieCustomizer(Consumer)instead.- Since:
- 5.2
 
- 
setSecureDeprecated.UsesetCookieCustomizer(Consumer)instead.- Since:
- 5.4
 
- 
setCookieMaxAgeDeprecated.UsesetCookieCustomizer(Consumer)instead.- Since:
- 5.5
 
 
- 
setCookieCustomizer(Consumer)instead.