美文网首页
基于Cookies的跨域设置

基于Cookies的跨域设置

作者: EasyNetCN | 来源:发表于2022-06-22 09:32 被阅读0次

    由于一些历史原因,项目是基于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的跨域设置

          本文链接:https://www.haomeiwen.com/subject/eqwyvrtx.html