【问题标题】:Sockets are always not authenticated套接字始终未经过身份验证
【发布时间】:2014-11-15 03:21:39
【问题描述】:

我刚刚配置了我的 express (4.x) + socket.io(1.x) + angular.js 应用程序,我的应用程序看起来像

快递

app.use(session({secret:"mysecret",store:new RedisStore({ host: 'localhost', port: 6379, client:       redis }), cookie: {httpOnly: false,secure: false}}));

Socket.io 配置

function onAuthorizeSuccess(data, accept){
    console.log('successful connection to socket.io');

    // If you use socket.io@1.X the callback looks different
    accept();
}

function onAuthorizeFail(data, message, error, accept){
    if(error)  throw new Error(message);
    return accept();
}

io.use(passportSocketIo.authorize({
    passport:passport,
    cookieParser: cookieParser,
    key:         'connect.sid',       // the name of the cookie where express/connect stores its     session_id
    secret:      'mysecret',    // the session_secret to parse the cookie
    store:       new RedisStore({ host: 'localhost', port: 6379, client: redis }),
    success:     onAuthorizeSuccess,  // *optional* callback on success - read more below
    fail:        onAuthorizeFail     // *optional* callback on fail/error - read more below
}));

一切正常,会话存储在redis中,我的快速应用程序显示req.isAutheticated() = true

但是:

io.sockets.on('connection', function (socket) {
    console.log(socket.request.user);


    socket.on('disconnect',function(){
        console.log('User disconnected');
    });
});

logged_in = false

我的角度代码看起来像(getCookie('connect.sid') 具有正确的值)

var socket = io.connect('ws://chipso.eu:3000/',{
    query: 'session_id=' + getCookie('connect.sid')
});

return {
    on: function (eventName, callback) {
        socket.on(eventName, function () {
            var args = arguments;
            $rootScope.$apply(function () {
                callback.apply(socket, args);
            });
        });
    },
    emit: function (eventName, data, callback) {
        socket.emit(eventName, data, function () {
            var args = arguments;
            $rootScope.$apply(function () {
                if (callback) {
                    callback.apply(socket, args);
                }
            });
        })
    }
};

我的 redis 商店看起来像

{"cookie":{"originalMaxAge":2591999999,"expires":"2014-10-20T19:06:23.097Z","secure":false,
"httpOnly":false,"path":"/"},"passport":{"user":{"id":1,"name":"Filip Lukáč","first_name":"Filip",
"last_name":"Lukáč","gender":"male","reg_date":"2014-09-15 11:57:34.079","username":"filip.lukac@gmail.com",
"role":"admin","photo":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/v/t1.0-1/p100x100/
10645292_334651976702411_6704623329146986982_n.jpg?oh=a5ed18ae84dfc3909b4bfb036a0a2b8d&
oe=548BC691&__gda__=1419568684_70705b96aaa90cf986992372925d442e","logged":true}}}

这是我的调试日志。

 _query: 
   { session_id: 's:uhckQqqa4XdGEtOHg0EwrumGdRDYoa9C.QEWbVG1/w3aqH7WJ97YSTisZuKlZvQd1rYJvV92L2Gs',
     EIO: '3',
     transport: 'polling',
     t: '1411242792055-0' },
  res: 
   { domain: null,
    _events: { finish: [Function] },
     _maxListeners: 10,
     output: [],
     outputEncodings: [],
     writable: true,
     _last: false,
     chunkedEncoding: false,
     shouldKeepAlive: true,
     useChunkedEncodingByDefault: true,
     sendDate: true,
     _headerSent: true,
     _header: 'HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: 101\r\nAccess-Control-Allow-Origin: *\r\nSet-Cookie: io=ET-vPrTRy078bMyiAAAD\r\nDate: Sat, 20 Sep 2014 19:53:10 GMT\r\nConnection: keep-alive\r\n\r\n',
     _hasBody: true,
     _trailer: '',
     finished: true,
     _hangupClose: false,
     socket: null,
     connection: null,
     statusCode: 200 },
  cleanup: [Function: cleanup],
  read: [Function],
  socketio_version_1: true,
  cookie: { 'connect.sid': 'uhckQqqa4XdGEtOHg0EwrumGdRDYoa9C' },
  sessionID: 's:uhckQqqa4XdGEtOHg0EwrumGdRDYoa9C.QEWbVG1/w3aqH7WJ97YSTisZuKlZvQd1rYJvV92L2Gs',
  user: { logged_in: false } }

未找到会话,{ logged_in: false }。我的onAuthorizeFail 中的消息说= No session found

我想不通,问题出在哪里...有人可以帮我吗?

我只是想看看我的用户是否通过了身份验证

【问题讨论】:

    标签: angularjs node.js passport.js socket.io-1.0


    【解决方案1】:

    编辑:

    我曾经创建拉请求。如果有人在使用 RedisStore,请检查一下。

    Pull-request

    问题出在passport.socketio 库中

    https://github.com/jfromaniello/passport.socketio/blob/master/lib/index.js#L57 在这里,当我试图在 RedisStore 中找到我的会话时。

    sessionID = data.query.session_id. 
    

    但会话由

    存储
    session.cookie[auth.key]. 
    

    所以我重写了这一行

    data.sessionID = (data.query && data.query.session_id) || (data._query && data._query.session_id) || data.cookie[auth.key] || '';
    

    data.sessionID = data.cookie[auth.key] || '';
    

    警告: 它可能仅针对 RedisStore() 解决。

    【讨论】:

    • 您的“修复”仅删除了从 data.query.session_iddata._query.session_id 读取会话 ID 的选项。如果它们以您的sessionID 开头为空,则无论如何都会从data.cookie[auth.key] 收到。
    • @vesse 是对的。如果您发送 session_id,这可以为您改变一些事情的唯一方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多