vue-admin-template与nginx,springboot出现的跨域问题

使用nginx代理时出现的跨域问题,

img


Access to XMLHttpRequest at 'http://localhost:9001/eduLoginController/user/login' from origin 'http://localhost:9528' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
request.js:55 errError: Network Error
xhr.js:178 POST http://localhost:9001/eduLoginController/user/login net::ERR_FAILED

springboot有没有做一下跨域拦截器或者过滤器配置,定义一个CorsConfig类实现WebMvcConfigurer然后加一个注解@Configuration交给springboot

img

nginx的conf文件要配置,然后controller要加@CrossOrgin注解

问题描述得不够清晰,所以这里提供一个完整的配置例子
假设:

请先确认以下前提条件:

  • spring boot controller添加了跨域注解

那么,ngnix配置如下(ngnix.conf):

server {
        listen       9003;
        server_name  localhost;

        location / {
            # 最后的斜杠别落下
            proxy_pass    http://localhost:9009/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        access_log  ./log/access.log;
    }

admin-templete配置(.env.development):

VUE_APP_BASE_API = 'http://localhost:9003'