Class DSpaceCsrfTokenRepository
- All Implemented Interfaces:
CsrfTokenRepository
This is essentially a customization of Spring Security's CookieCsrfTokenRepository: https://github.com/spring-projects/spring-security/blob/6.2.x/web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java However, as that class is "final" we aannot override it directly.
How it works: 1. Backend generates XSRF token & stores in a *server-side* cookie named DSPACE-XSRF-COOKIE. By default, this cookie is not readable to JS clients (HttpOnly=true). But, it is returned (by user's browser) on every subsequent request to backend. See "saveToken()" method below. 2. At the same time, backend also sends the generated XSRF token in a header named DSPACE-XSRF-TOKEN to client. See "saveToken()" method below. 3. Client MUST look for DSPACE-XSRF-TOKEN header in a response from backend. If found, the client MUST store/save this token for later request(s). For Angular UI, this task is performed by the XsrfInterceptor. 4. Whenever the client is making a mutating request (e.g. POST, PUT, DELETE, etc), the XSRF token is REQUIRED to be sent back in the X-XSRF-TOKEN header. * NOTE: non-mutating requests (e.g. GET, HEAD) do not check for an XSRF token. This is default behavior in Spring Security 5. On backend, the X-XSRF-TOKEN header is received & compared to the current value of the *server-side* cookie named DSPACE-XSRF-COOKIE. If tokens match, the request is accepted. If tokens don't match a 403 is returned. This is done automatically by Spring Security. In summary, the XSRF token is ALWAYS sent to/from the client & backend via *headers*. This is what allows the client and backend to be on different domains. The server-side cookie named DSPACE-XSRF-COOKIE is (usually) not accessible to the client. It only exists to allow the server-side to remember the currently active XSRF token, so that it can validate the token sent (by the client) in the X-XSRF-TOKEN header.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongenerateToken(jakarta.servlet.http.HttpServletRequest request) Method is copied fromCookieCsrfTokenRepository.generateToken(HttpServletRequest)Method is copied fromCookieCsrfTokenRepository.getCookiePath()loadToken(jakarta.servlet.http.HttpServletRequest request) Method is copied fromCookieCsrfTokenRepository.loadToken(HttpServletRequest)voidsaveToken(CsrfToken token, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method has been modified for DSpace.voidsetCookieCustomizer(Consumer<ResponseCookie.ResponseCookieBuilder> cookieCustomizer) Method is copied fromCookieCsrfTokenRepository.setCookieCustomizer(Consumer)voidsetCookieDomain(String cookieDomain) Deprecated.voidsetCookieHttpOnly(boolean cookieHttpOnly) Deprecated.UsesetCookieCustomizer(Consumer)instead.voidsetCookieMaxAge(int cookieMaxAge) Deprecated.UsesetCookieCustomizer(Consumer)instead.voidsetCookieName(String cookieName) Method is copied fromCookieCsrfTokenRepository.setCookieName(String)voidsetCookiePath(String path) Method is copied fromCookieCsrfTokenRepository.setCookiePath(String)voidsetHeaderName(String headerName) Method is copied fromCookieCsrfTokenRepository.setHeaderName(String)voidsetParameterName(String parameterName) Method is copied fromCookieCsrfTokenRepository.setParameterName(String)voidDeprecated.UsesetCookieCustomizer(Consumer)instead.static DSpaceCsrfTokenRepositoryMethod is copied fromCookieCsrfTokenRepository(and only modified to return the DSpaceCsrfTokenRepository instead)Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.security.web.csrf.CsrfTokenRepository
loadDeferredToken
-
Field Details
-
DEFAULT_CSRF_COOKIE_NAME
- See Also:
-
DSPACE_CSRF_HEADER_NAME
- See Also:
-
DEFAULT_CSRF_PARAMETER_NAME
- See Also:
-
DEFAULT_CSRF_HEADER_NAME
- See Also:
-
-
Constructor Details
-
DSpaceCsrfTokenRepository
public DSpaceCsrfTokenRepository()
-
-
Method Details
-
setCookieCustomizer
Method is copied fromCookieCsrfTokenRepository.setCookieCustomizer(Consumer) -
generateToken
Method is copied fromCookieCsrfTokenRepository.generateToken(HttpServletRequest)- Specified by:
generateTokenin interfaceCsrfTokenRepository
-
saveToken
public void saveToken(CsrfToken token, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method has been modified for DSpace. It borrows MOST of the logic fromCookieCsrfTokenRepository.saveToken(CsrfToken, HttpServletRequest, HttpServletResponse)It applies a "SameSite" attribute to every cookie by default.
It also sends the token (if not empty) back in BOTH the cookie and the custom "DSPACE-XSRF-TOKEN" header. By default, Spring Security will only send the token back in the cookie.
- Specified by:
saveTokenin interfaceCsrfTokenRepository- Parameters:
token- current tokenrequest- current requestresponse- current response
-
loadToken
Method is copied fromCookieCsrfTokenRepository.loadToken(HttpServletRequest)- Specified by:
loadTokenin interfaceCsrfTokenRepository
-
setParameterName
Method is copied fromCookieCsrfTokenRepository.setParameterName(String) -
setHeaderName
Method is copied fromCookieCsrfTokenRepository.setHeaderName(String) -
setCookieName
Method is copied fromCookieCsrfTokenRepository.setCookieName(String) -
setCookieHttpOnly
Deprecated.UsesetCookieCustomizer(Consumer)instead.Method is copied fromCookieCsrfTokenRepository.setCookieHttpOnly(boolean) -
withHttpOnlyFalse
Method is copied fromCookieCsrfTokenRepository(and only modified to return the DSpaceCsrfTokenRepository instead) -
setCookiePath
Method is copied fromCookieCsrfTokenRepository.setCookiePath(String) -
getCookiePath
Method is copied fromCookieCsrfTokenRepository.getCookiePath() -
setCookieDomain
Deprecated.UsesetCookieCustomizer(Consumer)instead.Method is copied fromCookieCsrfTokenRepository.setCookieDomain(String) -
setSecure
Deprecated.UsesetCookieCustomizer(Consumer)instead.Method is copied fromCookieCsrfTokenRepository.setSecure(Boolean) -
setCookieMaxAge
Deprecated.UsesetCookieCustomizer(Consumer)instead.Method is copied fromCookieCsrfTokenRepository.setCookieMaxAge(int)
-
setCookieCustomizer(Consumer)instead.