session - 单服务器、不同端口上的多个 Express 和 Express-Session 应用程序

标签 session express express-session

我正在单个服务器上的多个端口下使用快速 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 or 127.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 different names per app.

关于session - 单服务器、不同端口上的多个 Express 和 Express-Session 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37553043/

相关文章:

c# - 我正在尝试在 MVC 5 中使用 Session 来传递最近注册用户的 Id

php - Symfony3 : How to delete current user and redirect to home?

javascript - Node/Express - 如何实现 DELETE 和 PUT 请求

node.js - 如何使用 Node.js Express-Session 关闭单独的 session ?

javascript - 类型 `user` 上不存在属性 `Session & Partial<SessionData>`

php - 静态对象中的魔术方法

javascript - 使用 Express.js 从 Amazon S3 流式传输 Assets (JS/CSS/图像)

javascript - 当请求的凭据模式为 '*' 时,响应中的 Access-Control-Allow-Origin' header 不得为通配符 'include'

node.js - 为什么 CSRF token 要加密?

javascript - 在服务器端进行passport js本地用户身份验证后,如何在客户端使用cookie来显示用户信息?