我正在单个服务器上的多个端口下使用快速 session 运行多个 MEAN 应用程序。当我对应用程序 A 进行身份验证时,为另一个应用程序(应用程序 B)建立的 token 被修改。然后,应用程序 B 的经过身份验证的用户 session 将被拒绝。
如何配置我的所有应用程序以独立保存和验证 token ?例如,在本地主机上,应用程序 A 在端口 80 上运行,应用程序 B 在端口 90 上运行。我希望用户能够在不中断其他用户的应用程序 B session 的情况下验证和使用应用程序 A。
以下是我的 app.js 文件中与我的问题相关的代码:
// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);
var connection = mongoose.createConnection(config.mongo.uri, config.mongo.options);
var app = express();
// enable CORS
app.use(cors());
// enable cookieParser
app.use(cookieParser());
// enable session
app.use(session({
secret: config.secrets.session,
resave: false,
saveUninitialized: true,
name: 'uniqueSessionId',
store: new MongoStore(
{
mongooseConnection: connection
}
)
}));
最佳答案
Cookie(用于存储 session 标识符)在给定主机名上的所有端口之间共享,这就是您的应用程序相互干扰的原因。
express-session
的文档建议如下:
if you have multiple apps running on the same hostname (this is just the name, i.e.
localhost
or127.0.0.1
; different schemes and ports do not name a different hostname), then you need to separate the session cookies from each other. The simplest method is to simply set differentname
s per app.
关于session - 单服务器、不同端口上的多个 Express 和 Express-Session 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37553043/