Class CookieServerCsrfTokenRepository
java.lang.Object
org.springframework.security.web.server.csrf.CookieServerCsrfTokenRepository
- All Implemented Interfaces:
ServerCsrfTokenRepository
public final class CookieServerCsrfTokenRepository
extends Object
implements ServerCsrfTokenRepository
A
ServerCsrfTokenRepository 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:
- 5.1
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<CsrfToken>generateToken(org.springframework.web.server.ServerWebExchange exchange) Generates aCsrfTokenreactor.core.publisher.Mono<CsrfToken>loadToken(org.springframework.web.server.ServerWebExchange exchange) Loads the expectedCsrfTokenfrom theServerWebExchangereactor.core.publisher.Mono<Void>Saves theCsrfTokenusing theServerWebExchange.voidsetCookieCustomizer(Consumer<org.springframework.http.ResponseCookie.ResponseCookieBuilder> cookieCustomizer) Add aConsumerfor aResponseCookieBuilderthat will be invoked for each cookie being built, just before the call tobuild().voidsetCookieName(String cookieName) Sets the cookie namevoidsetCookiePath(String cookiePath) Sets the cookie pathvoidsetHeaderName(String headerName) Sets the header namevoidsetParameterName(String parameterName) Sets the parameter nameFactory method to conveniently create an instance that has creates cookies withResponseCookie.isHttpOnly()set to false.
-
Constructor Details
-
CookieServerCsrfTokenRepository
public CookieServerCsrfTokenRepository()
-
-
Method Details
-
setCookieCustomizer
public 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
-
withHttpOnlyFalse
Factory method to conveniently create an instance that has creates cookies withResponseCookie.isHttpOnly()set to false.- Returns:
- an instance of CookieCsrfTokenRepository that creates cookies with
ResponseCookie.isHttpOnly()set to false
-
generateToken
public reactor.core.publisher.Mono<CsrfToken> generateToken(org.springframework.web.server.ServerWebExchange exchange) Description copied from interface:ServerCsrfTokenRepositoryGenerates aCsrfToken- Specified by:
generateTokenin interfaceServerCsrfTokenRepository- Parameters:
exchange- theServerWebExchangeto use- Returns:
- the
CsrfTokenthat was generated. Cannot be null.
-
saveToken
public reactor.core.publisher.Mono<Void> saveToken(org.springframework.web.server.ServerWebExchange exchange, @Nullable CsrfToken token) Description copied from interface:ServerCsrfTokenRepositorySaves theCsrfTokenusing theServerWebExchange. If theCsrfTokenis null, it is the same as deleting it.- Specified by:
saveTokenin interfaceServerCsrfTokenRepository- Parameters:
exchange- theServerWebExchangeto usetoken- theCsrfTokento save or null to delete
-
loadToken
public reactor.core.publisher.Mono<CsrfToken> loadToken(org.springframework.web.server.ServerWebExchange exchange) Description copied from interface:ServerCsrfTokenRepositoryLoads the expectedCsrfTokenfrom theServerWebExchange- Specified by:
loadTokenin interfaceServerCsrfTokenRepository- Parameters:
exchange- theServerWebExchangeto use- Returns:
- the
CsrfTokenor null if none exists
-
setCookieName
Sets the cookie name- Parameters:
cookieName- The cookie name
-
setParameterName
Sets the parameter name- Parameters:
parameterName- The parameter name
-
setHeaderName
Sets the header name- Parameters:
headerName- The header name
-
setCookiePath
Sets the cookie path- Parameters:
cookiePath- The cookie path
-