跨子域共享 Express Session
Sharing Express Session Across Sub Domains
我有 2 个应用程序,WEB 和 APP,它们托管在不同的端口(本地)和不同的域(公共)上示例:
- www.domain.com(网页)
- app.domain.com(APP)
我正在尝试在两个应用程序上调用相同的登录会话,我的代码在我的本地计算机上运行良好(可能是因为它的相同主机名和端口不会中断会话并将其视为新的会话)
当我尝试将我的代码推送到生产环境时它根本不起作用,网络会话无法识别应用程序设置的会话
APP 是设置会话的地方(它处理登录)。
下面的 session 和 cors 代码在 web 和 app 上使用
const cors = require('cors')
app.use(cors());
app.use(cookieParser());
app.use(
session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: { maxAge: 1209600000 }, // two weeks in milliseconds
store: new MongoStore({
mongooseConnection: mongoose.connection,
autoReconnect: true
})
})
);
一旦用户从应用程序登录,我希望通过 WEB 包维护和召回该会话。这样我也可以动态更改网站体验。
解决方案
@theusguy 关于添加域参数是正确的。我面临更多需要解决的问题
Headers.Host
我的 nginx 配置不正确,所以主机名是 127.0.0.1:3000
为了解决这个问题,我将以下配置添加到 conf
location / {
proxy_pass http://127.0.0.1:3021;
proxy_http_version 1.1; //this was missing
proxy_set_header Upgrade $http_upgrade; //this was missing
proxy_set_header Connection 'upgrade'; //this was missing
proxy_set_header Host $host; //this was missing
proxy_cache_bypass $http_upgrade; //this was missing
}
继续前进,app.js 的流量很重要
应用程序
var app = express();
app.enable('trust proxy');
// app.use(cors()); //APP does not need cors because its GENERATING the long lasting session that needs to be read everywhere
//Everything else goes below this
app.use(cookieParser());
app.use(
session({
resave: false,
saveUninitialized: false,
secret: process.env.SESSION_SECRET,
cookie: {
domain: '.domain.com',
maxAge: 1209600000 // two weeks in milliseconds
},
store: new MongoStore({
mongooseConnection: mongoose.connection,
autoReconnect: true
})
})
);
var mainRouter = require('./routes/main');
网络
var app = express();
app.enable('trust proxy');
app.use(cors()); // TO read the cookie being set from elsewhere
var mainRouter = require('./routes/main');
最后
我不得不清除我的 cookies 以消除任何漏报
^ 这是最烦人的一步
这可能是由于在子域级别设置了 cookie。 app.domain.com(应用程序)的会话 cookie 对 www.domain.com(WEB)无效。
尝试更改代码以在域级别设置 cookie,看看是否可行。
app.use(
session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: { maxAge: 1209600000, domain:'.domain.com' },
store: new MongoStore({
mongooseConnection: mongoose.connection,
autoReconnect: true
})
})
);
我有 2 个应用程序,WEB 和 APP,它们托管在不同的端口(本地)和不同的域(公共)上示例:
- www.domain.com(网页)
- app.domain.com(APP)
我正在尝试在两个应用程序上调用相同的登录会话,我的代码在我的本地计算机上运行良好(可能是因为它的相同主机名和端口不会中断会话并将其视为新的会话)
当我尝试将我的代码推送到生产环境时它根本不起作用,网络会话无法识别应用程序设置的会话
APP 是设置会话的地方(它处理登录)。
下面的 session 和 cors 代码在 web 和 app 上使用
const cors = require('cors')
app.use(cors());
app.use(cookieParser());
app.use(
session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: { maxAge: 1209600000 }, // two weeks in milliseconds
store: new MongoStore({
mongooseConnection: mongoose.connection,
autoReconnect: true
})
})
);
一旦用户从应用程序登录,我希望通过 WEB 包维护和召回该会话。这样我也可以动态更改网站体验。
解决方案
@theusguy 关于添加域参数是正确的。我面临更多需要解决的问题
Headers.Host 我的 nginx 配置不正确,所以主机名是 127.0.0.1:3000 为了解决这个问题,我将以下配置添加到 conf
location / { proxy_pass http://127.0.0.1:3021; proxy_http_version 1.1; //this was missing proxy_set_header Upgrade $http_upgrade; //this was missing proxy_set_header Connection 'upgrade'; //this was missing proxy_set_header Host $host; //this was missing proxy_cache_bypass $http_upgrade; //this was missing }
继续前进,app.js 的流量很重要
应用程序
var app = express();
app.enable('trust proxy');
// app.use(cors()); //APP does not need cors because its GENERATING the long lasting session that needs to be read everywhere
//Everything else goes below this
app.use(cookieParser());
app.use(
session({
resave: false,
saveUninitialized: false,
secret: process.env.SESSION_SECRET,
cookie: {
domain: '.domain.com',
maxAge: 1209600000 // two weeks in milliseconds
},
store: new MongoStore({
mongooseConnection: mongoose.connection,
autoReconnect: true
})
})
);
var mainRouter = require('./routes/main');
网络
var app = express();
app.enable('trust proxy');
app.use(cors()); // TO read the cookie being set from elsewhere
var mainRouter = require('./routes/main');
最后
我不得不清除我的 cookies 以消除任何漏报 ^ 这是最烦人的一步
这可能是由于在子域级别设置了 cookie。 app.domain.com(应用程序)的会话 cookie 对 www.domain.com(WEB)无效。
尝试更改代码以在域级别设置 cookie,看看是否可行。
app.use(
session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: { maxAge: 1209600000, domain:'.domain.com' },
store: new MongoStore({
mongooseConnection: mongoose.connection,
autoReconnect: true
})
})
);