springboot+vue 解决跨域问题

1.后台添加过滤器,允许跨域
@Configuration
public class GlobalCorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("");
corsConfiguration.addAllowedHeader("
");
corsConfiguration.addAllowedMethod("*");

    UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
    configSource.registerCorsConfiguration("/**", corsConfiguration);

    return new CorsFilter(configSource);
}

}

2.前台
在config的index.js的proxyTable中加入拦截,拦截/api请求,替换成target中的请求,
比如原始请求是/api/hello.pub,则会替换成http://192.168.33.104:8086/hello.pub
proxyTable: {
'/api':{
target: 'http://192.168.33.104:8086',
changeOrigin:true,
pathRewrite:{
'^/api':''
}
}
},

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容