socket.io - 客户端安全性

时间:2018-06-18 14:51:17

标签: node.js express websocket socket.io passport.js

我对websocket安全性有疑问。例如,使用socket.io/express/passport进行聊天功能进行身份验证。像这样:

socket.emit('chatMessage', {
    msg: $('#m').val(),
    userid: '{{user.id}}'
});

这样安全吗?有人在发送邮件时会伪造自己的身份吗?如果是这样,那么更好的方法是什么。谢谢!

2 个答案:

答案 0 :(得分:0)

除非他发送你提供的令牌,否则这种方式并不安全。

最好的方法是在用户连接到服务器时对用户进行一次身份验证,而不是在每个.emit上发送凭据

io.use(async(socket, next) => {

    try {
        // Use cookies, or query with a token provided 
        // by oAUTH or any kind of authentication method, or whatever method you whish
        await authenticate(socket); 

        return next();

    } catch(err) {

        return next(new Error('Authentication error'));
    }

});

io.on('connection', socket => /* ... */);

如果auhtentication有效,则每个emit将来自经过身份验证的套接字。

答案 1 :(得分:0)

您使用nodejs socket.io,您可以在nodejs中安装cors,然后导入cors var cors = require(' cors');

你看:https://hoanguyenit.com/huong-dan-ket-hop-nodejs-vuejs.html https://hoanguyenit.com/chat-nodejs-vuejs.html