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

2023-02-12,,,,

前后端分离,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);
}
}

springboot 服务工程,前端服务调用接口报跨域错误的相关教程结束。

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

下载本文的Word格式文档,以方便收藏与打印。