Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3630

SpringBoot csrf handling for API methods [duplicate]

$
0
0

I have a SpringBoot webapp, and it's working OK for API users to access by first "logging in" via the URI http://localhost:8081/loginRest

My WebSecurityConfig looks like this:

    http.csrf().disable().authorizeHttpRequests((requests) -> requests                    .requestMatchers("/", "/home", "/css/**", "/js/**", "/img/**",          "/loginRest"                ).permitAll()                    .anyRequest().authenticated())            .formLogin((form) -> form                    .loginPage("/login").successHandler(authSuccessHandler)                    .permitAll())            .logout((logout) -> logout.permitAll());

If I now want to enable CSRF protection for my Forms, I change it as follows:

    http.csrf(Customizer.withDefaults()).authorizeHttpRequests((requests) -> requests                    .requestMatchers("/", "/home", "/css/**", "/js/**", "/img/**", "/loginRest", ...

Which works fine for my HTML pages and Forms, but also prevent POST requests to the /loginRest endpoint. What's the correct way to handle CSRF in this scenario? Do I need to make a GET request first, to read the CSRF value?


Viewing all articles
Browse latest Browse all 3630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>