【问题标题】:Google Chrome not setting cookie sent from NodeJS/Express serverGoogle Chrome 未设置从 NodeJS/Express 服务器发送的 cookie
【发布时间】:2017-03-06 22:26:43
【问题描述】:

我正在尝试在我的后端(Express/Node/Mongo with express-session)中设置经过身份验证的端点,并且可以在响应标头中向客户端(Chrome 版本 53.0.2785.143)发送 cookie,但是当我寻找开发控制台下的 cookie Application->Storage->Cookies->http://localhost:8100 它不存在,因此在随后的请求标头中没有任何内容发送回服务器。但是,当我测试使用 Postman 编写的代码时,似乎一切正常,这意味着服务器在登录时发送一个 cookie,并且当我 GET 身份验证的端点时返回 cookie。

Response Headers
HTTP/1.1 200 OK
X-Powered-By: Express
Vary: X-HTTP-Method-Override
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: DELETE, PUT, GET
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Content-Type: application/json; charset=utf-8
Content-Length: 1258
ETag: W/"4ea-X9Q0hp8ptccLVapzMZamYA"
set-cookie: connect.sid=s%3AyEaCZPUtH-rA0yQ3Osk-FNBHxQNYbFqp.gvwe%2FO0GSSfaX6i8Y29XD9vEo6ht2M%2FqL00wiFpntnU; Path=/
Date: Tue, 25 Oct 2016 01:28:59 GMT
Connection: keep-alive

Request Headers
POST /login HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Content-Length: 51
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36
Content-Type: application/json
Accept: */*
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

Request Payload
{"email":"test9876@gmail.com","password":"test123"}

Settings object for the session ID cookie:
{ path: '/', _expires: null, originalMaxAge: null, httpOnly: false }

session ID cookie name = 'connect.sid'.

Ionic2 service to login user.
public loginUser(user:Object):Observable<any>{
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:8000/login', JSON.stringify(user), {headers: headers})
            .map(this.extractData)
            .catch(this.handleError)
}

public extractData(res: Response) {
    console.log(res.headers); //cookie does not log here in response
    let body = res.json();
    return body || { };
}

【问题讨论】:

    标签: node.js google-chrome cookies google-chrome-devtools session-cookies


    【解决方案1】:

    通常,chrome 不会为 localhost 保存 cookie。请在 chrome 中禁用您的网络安全。

    How to disable chrome web security ?

    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:/Chrome dev session" --args --disable-web-security
    

    【讨论】:

    • 我正在运行 osX,所以我在终端中运行了它,但 cookie 仍未保存:$ open -a Google\ Chrome --args --disable-web-security --user-data-dir -enable-file-cookies
    • 好的,所以我设置会话选项如下,现在 Chrome 正在设置 cookie:app.use(session({ secret: 'your secret, store: new MongoStore({ mongooseConnection: mongoose.connection) }), cookie: { httpOnly: false, secure: false }, resave: false, saveUninitialized: false }));我为 domain 参数添加了一个值,但作为解决方案的一部分,我将其删除。
    • 好的,首先“禁用网络安全”对我来说不是一个可接受的解决方案,因为它还没有“准备好生产”。其次,chrome是不是只为“localhost”或“127.0.0.1”保存cookie,还是在其他主机名解析为127.0.0.1时也拒绝保存cookie?
    • @user2232681 很有趣。我遇到了与 express.js 完全相同的问题,只要我添加域,cookie 就会停止存储。但在我的情况下,我无法删除域,因为我需要它作为父域,例如由“my.example.com”提供服务时的“.example.com”
    猜你喜欢
    • 2021-08-28
    • 2016-10-15
    • 2013-03-02
    • 2016-07-01
    • 2022-08-23
    • 2017-06-07
    • 1970-01-01
    • 2015-09-21
    • 2012-04-08
    相关资源
    最近更新 更多