I am working on a Spring Boot application and facing an issue with the CSRF filter. Despite disabling CSRF in my security configuration, I keep receiving the error “Invalid CSRF token found” when trying to make a POST request to my registration endpoint.
Here is my SecurityConfig
class configuration:
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final AuthenticationProvider authProvider;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(authRequest -> authRequest
.requestMatchers("/auth/**").permitAll()
.anyRequest().authenticated())
.sessionManagement(sessionManager -> sessionManager
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authenticationProvider(authProvider)
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
}```java
I tried the following:
1. Disabled CSRF using csrf().disable() in my SecurityConfig.
2. Disabled CSRF using .ignoringRequestMatchers("/auth/**") in my SecurityConfig.
3. Made POST requests to /auth/register using Postman.
Despite these steps, I continue to receive a 401 error indicating an invalid CSRF token. I have also checked the server logs and confirmed that the CSRF filter is still active.