如何将应用程序配置为可用的子域 cookie?

How to configure application to be available subdomain cookie?

我需要在主域和所有子域之间共享会话 cookie。我有两个基于 expressjs 框架的 nodejs 服务:

// example.local

    ...
    app.use(session({
       cookie: {
          domain: "example.local"
       }
       , key: 'sid'
       , secret: '[my secret]'
       , saveUninitialized: true
       , resave: true
       , store: new RedisStore({
          host: 'localhost',
          port: 6379
       })
    })); 

// blog.example.local

    ...
    app.use(session({
       // what should I write here? <---------
    })); 

所以我的问题是我应该在 blog.example.local 的会话配置中写什么来访问 example.local 的现有 cookie?

编辑: 正如@yeiniel 建议的那样,我应该对 blog.example.local 使用相同的配置,如下所示:

// blog.example.local

    ...
    app.use(session({
       cookie: {
          domain: "example.local"
       }
       , key: 'sid'
       , secret: '[my secret]'
       , saveUninitialized: true
       , resave: true
       , store: new RedisStore({
          host: 'localhost',
          port: 6379
       })
    })); 

够用了还是我可以优化一下?

基本上你需要两件事:在所有服务器上使用相同的设置(不仅仅是 cookie 设置,所有会话设置都包括商店)并确保 cookie 域配置指向站点之间的公共域。

我目前正在管理类似的设置 所有应用都具有相同的会话设置

app.use(session({
store: redisStore,
secret: config.secret,
resave: true,
rolling: true,
saveUninitialized: false,
name: config.cookie_name,
cookie: {
  domain: config.cookie_domain_name, \ .website.tld
  secure: false
}

您将无法使用 localhost 来保存您的会话数据,特别是当应用程序位于不同的服务器上时。您将需要一个中央存储来存储会话数据,所有应用程序都可以访问它。

我觉得你的中间件中的cookie属性应该是这样的,

cookie: {
      domain: ".example.local",
      path:'/'
}

对于 blog.example.local

cookie: {
      domain: "example.local",
      path:'/'
}

对于example.local

希望这对你有用。