互联网上的许多示例只是说要打电话http.csrf()。disable(),但这最终会禁用所有身份验证(导致身份验证主体永远空值)。
这是在使用Spring Boot时如何禁用REST服务的CSRF保护而不禁用所有身份验证的方法。
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated()
.and().oauth2Login()
.and().csrf().disable();
}
}
from: https://dev.to//jbaranski/disable-csrf-while-using-oauth2-in-spring-boot-3hcm