在前后端分离后,为了解耦而有了跨域请求,而springboot中也有相关配置类可让我们配置。 1、通过CorsConfiguration 类配置全局的跨域请求配置
@Configuration
public class CorsConfig {
private CorsConfiguration buildconfig(){
CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
configuration.addAllowedOrigin("*");
configuration.setAllowCredentials(true);
return configuration;
}
@Bean
public CorsFilter corsFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",buildconfig());
return new CorsFilter(source);
}
}
2 、通过注解方式,但是这种方式需要在每个Controller中都配置,不过也可以指定更为具体的请求方式,请求地址等
@CrossOrigin(origins = "*",allowedHeaders = "*",methods = {},allowCredentials = "true")
|