Skip to content

Simplify CSRF Configuration for SPAs #14149

Closed
@jzheaux

Description

@jzheaux

For a SPA, the current recommendation for configuration CSRF is three-fold:

  1. set the CsrfTokenRepository to CsrfTokenRepository#withHttpOnlyFalse
  2. set the CsrfAttributeHandler to a custom class listed in the reference manual
  3. add a custom filter that "subscribes" to the deferred cookie so that the cookie header is written

The current state of the recommendation could be improved in a way that is less error-prone and requires less custom boilerplate for users.

One possibility is to provide a customizer like so:

.csrf(CsrfCustomizers.spaDefaults())

Where said customizer would apply these three rules for them. I imagine this might look something like the following:

public static Customizer<CsrfConfigurer> spaDefaults() {
    return (csrf) -> csrf
        .csrfTokenRepositorySubscription(CookieCsrfTokenRepository.withHttpOnlyFalse())
        .csrfTokenRequestHandler(new SpaCsrfTokenRequestHandler())
    )
}

Where csrfTokenRepositorySubscription is pseudocode for a way to supply the repository and indicate that the filter chain should automatically subscribe to the cookie as part of formulating the response (#3 in the above list) and SpaCsrfTokenRequestHandler is pseudocode for an implementation that is similar to the sample in the reference guide.

Metadata

Metadata

Assignees

Labels

in: webAn issue in web modules (web, webmvc)status: duplicateA duplicate of another issuetype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions