Skip to content

CSRF and test annotations #157

Answered by ch4mpy
ch4mpy asked this question in Q&A
Discussion options

You must be logged in to vote

It is expected behavior: test annotations are intended to populate the test security context with an Authentication instance at your hand. It do nothing about CSRF.

For CSRF, in addition to authentication annotation, use one of:

  • mockMvc.perform(post("/").with(SecurityMockMvcRequestPostProcessors.csrf()))
  • webTestClient.mutateWith(SecurityMockServerConfigurers.csrf())

As a reminder:

  • resource servers are generally stateless (without sessions), which make it insensible to CSRF (CSRF protection can be disabled when sessions are disabled) => you should have to care about CSRF only when using @WithOAuth2Login or @WithOidcLogin on an OAuth2 client with oauth2Login, not when using @WithJwt or @…

Replies: 1 comment

Comment options

ch4mpy
Dec 1, 2023
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by ch4mpy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant