使用node.js和Express-Session的跨域身份验证 [英] Cross Domain Authentication with node.js and express-session

查看:21
本文介绍了使用node.js和Express-Session的跨域身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Passport.js进行身份验证。Session Express-Session和Mongo-连接以存储它。我有个问题。它停止工作了。一直以来,一切都很好。不,不是。代码如下:

app.use(session({
    secret: 'secret',
    resave: true,
    // unset: 'destroy',
    domain: '.domain.com',
    saveUninitialized: false,
    cookie:  {
        // path: '/',
        domain: '.domain.com',
        maxAge: 24 * 6 * 60 * 10000
    },
    store: new MongoStore({url: config.db, autoReconnect:true})
}))

身份验证过程工作正常,但它不再适用于跨域。

有人有主意吗?已检查更新,但未找到任何内容。

推荐答案

只需像这样处理CORS效果,并在代码上方添加以下代码

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, authorization");
    res.header("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT,OPTIONS");
    next();
});

现在,在此中间件之后,您应该将代码放在如下位置:

app.use(session({
    secret: 'secret',
    resave: true,
    // unset: 'destroy',
    domain: '.domain.com',
    saveUninitialized: false,
    cookie:  {
        // path: '/',
        domain: '.domain.com',
        maxAge: 24 * 6 * 60 * 10000
    },
    store: new MongoStore({url: config.db, autoReconnect:true})
})) 

这篇关于使用node.js和Express-Session的跨域身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆