如何转发请求解决跨域

用vue和axios请求数据的时候出现了下面问题:
图片描述

然后找到了以下解决方案:
图片描述

但是我不知道这里的转发请求和代理究竟是什么意思,不懂这里面的思路?是要用到nginx吗?
哪位知道的麻烦讲一下?

阅读 6.5k
3 个回答

这里的代理是指 自己的前端页面,请求自己的后台,自己的后台用httpclient 去请求那个server。
如果那个server也是自己的,可以配置security设置允许跨域。

如果后台是自己开发的,那么只需在后台添加跨域支持就可以,不需要用到nginx
如果后台是SpringBoot
在继承了WebSecurityConfigurerAdapter的子类中添加跨域bean即可

    @Bean
    CorsConfigurationSource corsConfigurationSource()
    {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Collections.singletonList("*"));
        configuration.setAllowedMethods(Collections.singletonList("*"));
        configuration.setAllowedHeaders(Arrays.asList("Content-Type","Authorization"));
        configuration.setMaxAge((long) 600000);
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

springmvc 跨域

    <!-- API 接口跨域配置 -->
    <mvc:cors>
        <mvc:mapping path="/**"
             allowed-origins="*"
             allowed-methods="POST, GET, OPTIONS, DELETE, PUT"
             allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
             allow-credentials="true" />
    </mvc:cors>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题