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);
    }
}

 

posted @ 2018-06-07 17:18  漫步CODE人生  阅读(439)  评论(0编辑  收藏  举报