Skip to content

Commit

Permalink
Fix CSRF Token
Browse files Browse the repository at this point in the history
  • Loading branch information
schnapster committed Jan 14, 2024
1 parent 77a0c2d commit 01c266e
Showing 1 changed file with 6 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler;
import org.springframework.security.web.header.HeaderWriter;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
Expand Down Expand Up @@ -126,11 +127,16 @@ public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospe
this.oAuth2Config.getBaseRedirectUrl(),
this.privacyService
);
CsrfTokenRequestAttributeHandler requestHandler = new CsrfTokenRequestAttributeHandler();
// Force CSRF Token creation with Spring Security v6
// See https://docs.spring.io/spring-security/reference/5.8/migration/servlet/exploits.html#servlet-defer-loading-csrf-token-opt-out
requestHandler.setCsrfRequestAttributeName(null);

return http
.csrf(csrf -> csrf
.ignoringRequestMatchers(machineEndpoints)
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.csrfTokenRequestHandler(requestHandler)
)
.authorizeHttpRequests(auth -> auth
.requestMatchers(securedEndpoints).authenticated()
Expand Down

0 comments on commit 01c266e

Please sign in to comment.