vue打包成dist后,请求远程服务器java后端,报跨域。已配置 nginx映射,无法解决问题

后端已经部署在远程服务器使用 nginx 映射

后端部署在 8090, nginx使用 8089映射
浏览器url, 或 ajax 能够请求。
Vue axios打包后无法请求,打包前配置vue代理可以请求

img

报错

img

如果你的前端项目部署在8089端口,然后又将8089端口所监听到的请求全部代理到8090端口明显是不对的,这样没办法区分哪些是前端请求哪些是后端请求。

后端请求加这个试试 @CrossOrigin
或者

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CrossOriginConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true);
        WebMvcConfigurer.super.addCorsMappings(registry);
    }
}

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^