NodeJS、VueJS、快速会话。 Cookie 不在客户端设置

NodeJS, VueJS, express-session. Cookies don't set on client side

我使用的应用程序已经拥有自己的基础架构。任务是集成会话cookie机制。我花了很多时间来理解为什么 cookie 没有在客户端设置。

我。简而言之。

应用设置:

服务器:NodeJS 端口:8081

客户端:VueJS 端口:8088

我使用模块"express-session"在服务器端初始化会话机制并向客户端发送cookie。客户没有设置cookies。

二.详情:

服务器的根文件是index.js.

我在其中做了以下操作:

插入express模块:

const express = require('express')
  1. 插入cors模块:
const cors = require('cors')
  1. 添加cors设置:
app.use(cors({
    origin: 'http://localhost:8088',
    credentials: true
}))

然后我在user.js文件中初始化会话并接收客户端的连接: 插入 express-session 模块:

const session = require('express-session')
  1. 通过express.Router()插入路由:
const router = express.Router()
  1. 添加会话设置:
const EIGHT_HOURS  = 1000 * 60 * 60 * 2
const {
    SESS_NAME = 'sid',
    SESS_LIFETIME = EIGHT_HOURS,
    SESS_SECRET = 'test',
    NODE_ENV = 'development'
} = process.env
const IN_PROD = NODE_ENV === 'production'
  1. 初始化会话:
router.use(session({
    name: SESS_NAME,
    resave: false,
    saveUninitialized: false,
    secret: SESS_SECRET,
    cookie: {
        maxAge: SESS_LIFETIME,
        sameSite: false,
        // Must have HTTPS to work 'secret:true'
        secure: IN_PROD
    }
}))
  1. 通过router.post()
  2. 接收客户查询

App客户端由很多文件组成。客户端通过Axios模块向NodeJS服务器发送数据。

我阅读了几篇关于这个主题的文章,我想我所做的服务器端设置足以支持会话 cookie 机制。这意味着,问题出在 Vue 方面。

我做了什么:

  1. 我在axios向服务器发送数据的所有文件中设置了参数withCredentials为真值(withCredentials: true)来通过CORS限制。这没有帮助

  2. 生产中的应用程序还有其他 URLs 用于访问生产 NodeJS 服务器。我在所有客户端文件中设置了 develop NodeJS server URL。这没有帮助

  3. 阅读这篇文章:Vue forum. From this article I understood, that need to solve this problem by axios.interceptors ()。我想如果这个设置设置在客户端的一个页面上,cookie 可能至少应该在这个页面上工作。这没有帮助。 试图设置这样的设置:

axios.defaults.withCredentials = true

还有那个:

axios.interceptors.request.use( function (config) {
                console.log('Main interceptor success')
                config.withCredentials = true;
                return config;
            },
                function(error) {
                    // Do something with request error
                    console.log('Main interceptor error')
                    return Promise.reject(error);
                }
            )

这没有帮助

请告诉我应该朝哪个方向移动?是这样吗,在客户端绝对所有页面都必须 axios.defaults.withCredentials = true 设置来初始化 cookies 机制?我错过了什么细节?如果我从头开始设置会话 cookie,该机制就会起作用。

我解决了这个问题。我需要在另一个浏览器位置寻找 cookie 存储:

Chrome server cookie storage