【问题标题】:Cors not physically saving cookieCors 没有物理保存 cookie
【发布时间】:2017-08-29 18:48:46
【问题描述】:

经过数小时的挖掘和测试,我让本地前端 (angular2) 与开发后端对话。 在 https 请求和 htaccess 上使用 withCredentials: true

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"

在我重新加载页面之前一切都很好 - 我必须再次登录。
这是随请求一起发送的,但 cookie 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找不到了。

【问题讨论】:

    标签: apache http https cors


    【解决方案1】:

    在 webpack 设置中设置代理:

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

    与 withCredentials: true 一起解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2014-11-07
      • 1970-01-01
      • 2015-04-14
      • 2013-04-23
      • 2019-11-04
      • 2017-09-23
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多