【问题标题】:Cross Domain Authentication with node.js and express-session使用 node.js 和 express-session 进行跨域身份验证
【发布时间】:2016-10-05 09:33:18
【问题描述】:

我正在使用 Passport.js 进行身份验证。 Session Express-Session 和 Mongo-Connect 来存储它。我有问题。它停止工作。一直以来一切都很好。不,这不对。这是代码:

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 dns subdomain


    【解决方案1】:

    只需像这样处理 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})
    })) 
    

    【讨论】:

    • 这正是我的代码中的内容。这只是会话,与整体的跨功能无关。但我几分钟前就想通了。我更新了我所有的包,只是因为我在解决问题的过程中的某个地方。不知道是更新还是其他一些小改动:/ 感谢您的宝贵时间!
    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 2015-08-21
    • 2012-01-13
    • 2017-02-18
    • 2011-11-20
    • 2016-01-05
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多