localhost请求标头不发送cookie

时间:2014-06-24 14:30:29

标签: ajax node.js

我正在构建一个nodeJS服务器,允许用户使用客户端应用程序上的AJAX帖子进行登录。服务器响应cookie,成功登录后跟踪用户。但是我似乎无法让客户端应用程序将cookie发送回服务器。它似乎永远不会响应服务器,包括刚才收到的cookie。

在对服务器进行的第一次调用中,我使用我的凭据登录。服务器响应:

在回复标题中:

Set-Cookie:SID=0xtW36rYCiV; path=/; expires=Tue, 24-Jun-2014 14:14:51 GMT; secure

在后续的请求标题中     没有cookie被发送回服务器

在我的客户端应用程序中,我使用以下代码:

jQuery.ajax( {
            url: this.domain + this.url,
            async: this.async,
            type: 'POST',
            dataType: this.dataType,
            crossDomain: true,
            cache: true,
            accepts: 'text/plain',
            data: this.postVars,
            success: this.onData.bind( this ),
            error: this.onError.bind( this ),
            xhrFields: {
                withCredentials: true                   
            }
        });

注意我使用的是xhrFields。

在我的节点服务器中,我正在响应:(注意我包括所有CORRS变量)

if ( request.headers.origin )
{
    if ( request.headers.origin.match( /mydomain\.net/ ) || request.headers.origin.match( /appname\.mydomain\.com/ ) || request.headers.origin.match( /localhost/ )
                || request.headers.origin.match( /localhost\.com/ )
                || request.headers.origin.match( /localhost\.local/ )
                || request.headers.origin.match( /localtest\.com/ ) )
            {
                response.setHeader( 'Access-Control-Allow-Origin', request.headers.origin );
                response.setHeader( 'Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS' );
                response.setHeader( 'Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With' );
                response.setHeader( "Access-Control-Allow-Credentials", "true" );
            };
        }

        response.writeHead( 200, { "Content-Type": "application/json" });
        response.end( JSON.stringify( json ) );
}

我还编辑了我的windows hosts文件以包含这些测试域,这样我就不必使用IP或localhost:

127.0.0.1       localhost
127.0.0.1       tooltest.com
127.0.0.1       localhost.com
127.0.0.1       localhost.local

但无论我做什么,或者我使用的上述主机中的哪一个,它似乎都无法工作。它似乎只通过ajax与localhost相关,因为如果我直接进入有问题的服务器url - 它可以工作。

编辑1

所以例如 - 我打开客户端应用程序并尝试通过foo.com/user/log-in登录服务器。请求和响应的标头如下:

请求:

Remote Address:188.xxx.xxx.xx:7000
Request URL:https://foo.com:7000/user/log-in
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:undefined
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:45
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:foo.com:7000
Origin:http://localhost
Referer:http://localhost/animate-ts/trunk/bin/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Form Dataview sourceview URL encoded
user:mat
password:testpassword
rememberMe:true

响应:

Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type, Authorization, Content-Length, X-Requested-With
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin:http://localhost
Connection:keep-alive
Content-Type:application/json
Date:Tue, 24 Jun 2014 14:14:21 GMT
Set-Cookie:SID=0xtW36rYCiV; path=/; expires=Tue, 24-Jun-2014 14:14:51 GMT; secure
Transfer-Encoding:chunked

正如我所说,Foo.com告诉浏览器存储cookie 0xtW36rYCiV。但是,当我发出下一个请求(foo.com/user/is-logged-in)以查看用户是否已登录时,不会向foo发送任何cookie。 Foo.com查找ID为0xtW36rYCiV的cookie,但找不到任何内容。当我在开发工具中查看第二个请求时,我可以看到:

enter image description here

有没有人有任何其他想法?我以为我已经涵盖了上面的所有内容,但我开始认为它不会起作用:(

由于 垫

3 个答案:

答案 0 :(得分:1)

我的cookie没有标记为安全,并且在尝试从http:// localhost:3000 /向我的服务器(https://auth.mywebsite.com)发送GET请求时遇到了这个问题。

我在这里具有与OP相同的身份验证流程。 首先,登录auth.website.com,然后在响应标头中接收回cookie:

set-cookie: Authorization=Bearer%20blahblah.blahblah.blahblah; Path=/; Expires=Wed, 05 Aug 2020 02:07:57 GMT; HttpOnly

注意,我在Secure中没有set-cookie标志。

我注意到在Chrome devtools中,当悬停在set-cookie行上时,会弹出以下警告:

“此Set-Cookie未指定“ SameSite”属性,默认为“ SameSite = Lax”,并被阻止,因为它来自跨站点响应,而不是对顶级导航的响应。必须将Set-Cookie设置为“ SameSite = None”才能启用跨站点使用。”

这可能是我的问题的答案。但是,我不明白为什么这在昨天却没有奏效。两者之间没有代码更改。

Chrome和Safari上均会出现此问题。

MDN docsSet-Cookie的所有标志都有很好的描述。

这不是一个完整的答案,但是可能为其他面临类似问题的人提供一些额外的背景信息。

答案 1 :(得分:0)

我遇到了这个问题,但是@aspillers的评价为我找到了解决方案。 cookie被标记为安全,但是我正在通过HTTP而不是HTTPS进行请求。一旦调试器切换到HTTPS,它便开始工作。

答案 2 :(得分:0)

对于铬:

  • 转到 chrome://flags/
  • 禁用“SameSite 默认 cookie”
  • 重启 Chrome