2017-04-04 51 views
1

经过数小时的挖掘和测试,我得到了我的本地前端(angular2)与dev后端进行通信。 使用withCredentials: true对HTTPS请求和htaccess的:Cors不物理存储cookie

Header add Access-Control-Allow-Origin "https://127.0.0.1:3000" 
Header add Access-Control-Allow-Credentials "true" 
Header add Access-Control-Allow-Methods "GET, POST" 

这一切都很正常,直到我重新加载页面 - 我需要再次登录。
这与请求发送,但饼干PHPSESSID实际上不是保存在磁盘上:

Cookie:PHPSESSID=jp65lr9tviq6n5q9s1i0fupuq7 

如果我建立的代码,并上传到后端是,不使用CORS在同一台服务器 - 这一切工作正常,但当我在本地开发前端时,我无法做到这一点。

响应头:

Access-Control-Allow-Credentials:true 
Access-Control-Allow-Methods:GET, POST 
Access-Control-Allow-Origin:https://127.0.0.1:3000 
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Connection:Keep-Alive 
Content-Encoding:gzip 
Content-Length:105 
Content-Type:text/html 
Date:Tue, 04 Apr 2017 14:15:56 GMT 
Expires:Thu, 19 Nov 1981 08:52:00 GMT 
Keep-Alive:timeout=5, max=100 
Pragma:no-cache 
Server:Apache/2.4.7 (Ubuntu) 
Vary:Accept-Encoding 
X-Powered-By:PHP/5.5.9-1ubuntu4.20 

请求报头:

Accept:application/json, text/plain, */* 
Accept-Encoding:gzip, deflate, br 
Accept-Language:en-GB,en;q=0.8 
Connection:keep-alive 
Content-Length:62 
Content-Type:application/x-www-form-urlencoded 
Cookie:PHPSESSID=cjoe07094u4139i7c0pb4cd3e7 
Host:dev.server.com 
Origin:https://127.0.0.1:3000 
Referer:https://127.0.0.1:3000/login 
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 

缺少什么我在这里?

更新: 它似乎确实保存它 - 在错误的领域。 Cookie设置在api服务器域下,localhost无法再找到它。

回答

1

设置代理中的WebPack设置:

proxy: { 
    '/authenticate': { 
     target: 'https://dev.server.com', 
     secure: false, 
     changeOrigin: true 
    }, 
    '/Api': { 
     target: 'https://dev.server.com', 
     secure: false, 
     changeOrigin: true 
    } 
    } 

加上withCredentials:真正得到解决此问题。