节点 JS 会话不持久 [英] Node JS Session not persisting

查看:32
本文介绍了节点 JS 会话不持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 NodeJS 中设置用户会话时遇到了一些噩梦.我已经用谷歌搜索了它的生活,并在这里阅读了大量文章,但没有一个解决方案适合我.

I'm having a bit of a nightmare with setting up user sessions in NodeJS. I have googled the life out of it and read a whole host of articles on here but none of the solutions work for me.

我的设置:

app.use(cookieParser());
    app.use(session({
        pool: true,
        key: 'cgtracker.cookie',
        resave: false,
        saveUninitialized: false,
        secret: '1234567890QWERTY',
        cookie: {maxAge: 100000},
        store: new MySQLStore(options),
    }));

我正在使用 MySQLStore ('express-mysql-session').这链接到我的数据库并按预期工作.运行此 ->

I am using MySQLStore ('express-mysql-session'). This is linked to my Db and is working as expected.Running this ->

router.post('/Login', function (req, res) {
        logger.log("info", "Attempting Login for user: " + req.body.Username);
        req.session.username = req.body.Username;
        res.send('Created session for: ' + req.body.Username);

在我的会话表中为一个过期的会话创建一个条目.

Creates an entry in my session table for a session with an expiry.

| 2gbIWNuFVcE3GmjrFMEctdZlvBMufqiN | 1457713316 | {"cookie":{"originalMaxAge":100000,"expires":"2016-03-11T16:21:55.580Z","httpOnly":t                                                      rue,"path":"/"},"username":"chris.rayner"} 

我的问题是我无法在任何其他函数中检索任何会话.一个简单的测试在这里:

My problem is that I can't retrieve any session in any other function. A simple test here:

router.get('/', function (req, res) {
        logger.log("info", "Current Session: " + JSON.stringify(req.session));
}

我收到:

Current Session: {\"cookie\":{\"originalMaxAge\":100000,\"expires\":\"2016-03-11T16:24:15.212Z\",\"httpOnly\":true,\"path\":\"/\"}}","timestamp":"2016-03-11T16:22:35.212Z"}`

我的会话数据去哪儿了?!

我觉得我遗漏了一些明显的东西,但我尝试了很多研究的变化,我有点迷失了.

I feel like I'm missing something obvious, but I have tried so many variations from research I'm becoming a little lost.

我的浏览器 cookie 构造正确,但该值似乎与会话表中存储的任何 SessionID 无关.

My browser cookie is constructed correctly, though the value doesn't seem to correlate to any of the SessionID's stored in the session table.

非常感谢任何帮助/想法/建议!

Any help/ideas/suggestions would be very much appreciated!

克里斯

推荐答案

找到解决方案!

Chrome(也可能是 firefox,但未经测试)在发布到未知"域时,默认情况下会阻止 POST 请求中的 cookie.客户端 CORS 中的一些安全功能...

Chrome (and maybe firefox, but not tested) by default blocks cookies in POST requests when posting to a "unknown" domain. Some security feature in clientside CORS...

要修复此服务器端:- 使用授权来源更新 Cors,供浏览器检查.

To fix this server side: -Update Cors with an authorized origin(s) for the browser to check against.

app.use(cors({原点:config.origin,*(我使用一个数组来表示多个允许原点)*凭据:真实}));

要修复此客户端:将此添加到函数的 POST 标头中的选项中.

To fix this Client Side: Add this to the options in the POST header for a function.

xhrFields: {withCredentials: 真}

将此添加到类的模型构造函数中.

Add this to the Model Constructor for a Class.

$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {options.xhrFields = {withCredentials: 真};});

此后,在 POST 请求中创建的 Cookie 成功粘"到浏览器,会话功能就可以使用了!

After this, Cookies made in POST requests successfully "Stick" to the browser and Session functionality is a go!

好痛!希望这个解决方案可以帮助其他人在他们的项目上节省一些时间!

What a pain! Hope this solution helps somebody else save some time on their project!

这篇关于节点 JS 会话不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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