在你的如下类中
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // 启用方法级别的权限认证
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
加入如下方法:
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
configuration.setAllowCredentials(false);//是否支持安全证书(必需参数)
configuration.addAllowedOrigin(CorsConfiguration.ALL); //允许任何域名
configuration.addAllowedHeader(CorsConfiguration.ALL); //允许任何请求头
configuration.addAllowedMethod(CorsConfiguration.ALL); //允许任何请求方法
source.registerCorsConfiguration("/**", configuration);//访问路径
return source;
}
同时在http访问控制中配置:
import static org.springframework.security.config.Customizer.withDefaults;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.***
.***
.and().csrf().disable().cors(withDefaults()); //关闭cors,这行必须要有
}
然后就解决了。。。