简体   繁体   English

自定义 cookie 发送到浏览器但未存储

[英]Custom cookie sent to browser but not stored

I've tried multiple configuration and read multiple posts about this issue but I'm still lost.我已经尝试了多种配置并阅读了有关此问题的多篇文章,但我仍然迷路了。

The custom cookie sent by my api server is received by the browser but never stored.我的 api 服务器发送的自定义 cookie 被浏览器接收但从未存储。 And I can't figure out why.我不知道为什么。

I developed a node/express api server deployed on heroku.我开发了一个节点/express api 服务器,部署在 heroku 上。

My front-end uses Vuejs and is deployed on firebase.我的前端使用Vuejs,部署在firebase上。

Here are the code related to my cookie configuration:以下是与我的 cookie 配置相关的代码:

SERVER-SIDE服务器端

Cors Cors

app.use(
    cors({
        origin: 'https://my-app.firebaseapp.com',
        credentials: true,
    exposedHeaders: ['customCookie']
    })
);

Set-cookie设置cookie

const cookieOptions = {
    httpOnly: true,
    sameSite: 'None',
    expires: expirationDate,
    secure: true,
    path: '/',
    domain: 'https://my-app.firebaseapp.com',
};

res.cookie('customCookie', 'value', cookieOptions)

CLIENT-SIDE客户端

Axios Axios

axios.defaults.baseURL = 'https://my-app.herokuapp.com';
axios.defaults.withCredentials = true;

REQUEST HEADER请求 HEADER

POST /auth/sign-in HTTP/1.1
Host: my-app.herokuapp.com
Connection: keep-alive
Content-Length: 55
Pragma: no-cache
Cache-Control: no-cache
Origin: https://my-app.firebaseapp.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3921.0 Safari/537.36
Content-Type: application/json;charset=UTF-8
Accept: */*
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: cors
Referer: https://my-app.firebaseapp.com/auth/sign-in
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,nl;q=0.6,la;q=0.5

RESPONSE HEADER响应 HEADER

HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Dns-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-Xss-Protection: 1; mode=block
Access-Control-Allow-Origin: https://my-app.firebaseapp.com
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: customCookie
Set-Cookie: customCookie=value; Domain=https://my-app.firebaseapp.com; Path=/; Expires=Sun, 17 Nov 2019 11:54:30 GMT; HttpOnly; Secure; SameSite=None
Content-Type: application/json; charset=utf-8
Content-Length: 200
Etag: W/"c8-qlzfwyZ+uJQMQIeJOQnFSQYPR6o"
Date: Sun, 20 Oct 2019 11:54:30 GMT
Via: 1.1 vegur

So, the cookie is correctly sent and received here:因此,cookie 在此处正确发送和接收:

Set-Cookie: customCookie=value; Domain=https://my-app.firebaseapp.com; Path=/; Expires=Sun, 17 Nov 2019 11:54:30 GMT; HttpOnly; Secure; SameSite=None

But when I check the tab: Google Dev Tools >> Application >> Cookies >> my-app...但是当我检查选项卡时:Google Dev Tools >> Application >> Cookies >> my-app...

=> There is no cookie stored and therefore the cookie is not sent on subsequent request... => 没有存储 cookie,因此不会在后续请求中发送 cookie...

Any idea/advice?有什么想法/建议吗?

Thank you very much for your help,非常感谢您的帮助,

Romain罗曼

I think you need to handle Access-Control .我认为您需要处理Access-Control

you can do it by adding this code您可以通过添加此代码来做到这一点

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Origin', req.headers.origin);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
    next();
});

Also, I suppose that you are using Chrome as a browser, can you run chrome without security by doing this and then check if your cookies stored or not另外,我想您正在使用 Chrome 作为浏览器,您可以通过这样做在没有安全性的情况下运行 chrome,然后检查您的 cookies 是否存储

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

I think I get it.我想我明白了。

In Chrome (and maybe other navigator), cookies seems to be stored only in the domain address and the domain address, in my case, should be the server app (my-app.herokuapp.com), not the client one (my-app.firebaseapp.com).在 Chrome(可能还有其他导航器)中,cookies 似乎只存储在域地址中,在我的情况下,域地址应该是服务器应用程序(my-app.herokuapp.com),而不是客户端应用程序(my- app.firebaseapp.com)。

In other words, I thought my cookie would be stored and visible in my client-app address but they are actually stored and visible on my domain address.换句话说,我认为我的 cookie 将存储在我的客户端应用程序地址中并且可见,但它们实际上存储在我的域地址中并且可见。

Now everything works...现在一切正常...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cookie 未存储在浏览器中 - cookie NOT stored in browser 为什么浏览器没有设置从我的 node.js 后端发送的 cookie? - Why browser is not setting the cookie sent from my node js backend? 在 koa 服务器中检查日志时,不会从浏览器(本地主机)发送 cookie - cookie is not sent from browser (localhost) when check logs in koa server 为什么 express session cookie 在生产环境中没有存储在浏览器的 cookie 存储中,而是在 localhost/development 上工作? (表达+反应) - Why is the express session cookie not stored in the browser's cookie storage on production but is working on localhost/development? (express + react) 为什么每次请求都会发送 cookie,但在浏览器控制台或 document.cookie 中看不到? - Why are the cookies are being sent with every request but not visible in browser console or with document.cookie? 从后端 API (nodeJS express) 发送到 forntend (NextJS) 的 Cookie 未在浏览器中设置 - Cookie sent from backend API (nodeJS express) to forntend (NextJS) is not being set in the browser 快速会话发送的 Set-Cookie 未在浏览器上设置,但在本地开发中有效 - Set-Cookie sent by express-sessions is not being set on browser but works in local development 服务器发送的cookie不会覆盖现有的cookie - server sent cookie is not overriding the existing cookie Cookie 发送到 localhost 但未发送到 https 域 - Cookie sent to localhost but not to https domain 为什么cookie没有被发送到服务器 - Why is cookie not getting sent to server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM