springboot 服务工程,前端服务调用接口报跨域错误

时间:2021-08-02 14:20:41

前后端分离,VUE.JS调用服务接口时,跨域错误。需要服务接口工程设置,如下:

@SpringBootApplicationpublic class SpringCloudOpenapiApplication
{ public static void main(String[] args)
{
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
SpringApplication.run(TwSpringCloudOpenapiUcApplication.class, args);
} private CorsConfiguration buildConfig()
{
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addExposedHeader("Accept-Ranges");
corsConfiguration.addExposedHeader("Content-Range");
corsConfiguration.addExposedHeader("Content-Encoding");
corsConfiguration.addExposedHeader("Content-Length");
corsConfiguration.addExposedHeader("Authorization");
return corsConfiguration;
} /**
*
* @return
*/
@Bean
public CorsFilter corsFilter()
{
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); //
return new CorsFilter(source);
}
}