基于Cookies的跨域设置

由于一些历史原因,项目是基于cookies做的单点登录,在新的做的后台项目,使用了前后端分离架构,以下是在部署的时候,需要做的一些配置。

axios配置

axios.defaults.withCredentials = true;

const service = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  withCredentials: true,
  timeout: 50000,
  headers: { 'Content-Type': 'application/json;charset=utf-8' },
});

nginx配置

    server {
        listen 443 ssl;
        location / {
            add_header Access-Control-Allow-Origin $http_origin;
            add_header Access-Control-Allow-Headers $http_access_control_request_headers;
            add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
            add_header Access-Control-Allow-Credentials true;
            add_header Access-Control-Max-Age 3600;
        
            if ($request_method = OPTIONS){
                return 200;
            }
            
            proxy_set_header   Host             $host:$server_port;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
    }

你可能感兴趣的:(基于Cookies的跨域设置)