springboot 服务工程,前端服务调用接口报跨域错误
前后端分离,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()); // 4 return new CorsFilter(source); } }
编程PDF电子书免费下载: http://www.shitanlife.com/code 每天学习一点点