问题描述
我可以在没有密码的情况下将我的 Node 应用程序连接到 Redis,但是当我添加密码时,我所做的一切都是正确的.
I can connect my Node app to Redis just fine without a password, but when I add a password, nothing I do is right.
这是我现在的代码,取自示例::>
Here's my code right now, taken right from an example:
var redis = require('redis')
, sio = require('socket.io')
, RedisStore = sio.RedisStore
, io = sio.listen();
var port = 6379
, hostname = 'localhost'
, password = 'password';
var redisClient = redis.createClient(port, hostname);
redisClient.auth(password, function (err) { if (err) throw err; });
var redisSubscriber = redis.createClient(port, hostname);
redisSubscriber.auth(password, function (err) { if (err) throw err; });
io.set('store', new RedisStore({ redisPub: redisClient, redisSub: redisSubscriber, redisClient: redisClient }));
在运行应用程序时,我得到了这个堆栈跟踪:
On running the app, I get this stack trace:
/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/index.js:506
throw callback_err;
^
Error: Ready check failed: ERR operation not permitted
at RedisClient.on_info_cmd (/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/index.js:319:35)
at Command.RedisClient.ready_check.send_anyway [as callback] (/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/index.js:367:14)
at RedisClient.return_error (/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/index.js:502:25)
at RedisReplyParser.RedisClient.init_parser (/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/index.js:262:14)
at RedisReplyParser.EventEmitter.emit (events.js:93:17)
at RedisReplyParser.send_error (/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/lib/parser/javascript.js:266:14)
at RedisReplyParser.execute (/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/lib/parser/javascript.js:125:22)
at RedisClient.on_data (/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/index.js:478:27)
at Socket.<anonymous> (/home/eric/christmas/sockets/node_modules/socket.io/node_modules/redis/index.js:79:14)
at Socket.EventEmitter.emit (events.js:93:17)
生成此行的行是最后一行 - 如果我将设置 RedisStore 的尝试注释掉,则不会出现任何错误.
The line generating this is the final one - if I comment out the attempt to set the RedisStore, I do not get any errors.
我确定密码是正确的(我可以在 redis-cli 中验证它,如果我将密码更改为错误,我可以验证 auth 回调不会触发).如果我删除密码并注释掉两个 auth 行,此代码也有效.
I'm sure the password is right (I can verify it in redis-cli, and if I change the password to be wrong I can verify that the auth callbacks don't fire). This code also works if I remove the password and comment out the two auth lines.
博客文章和文档等上的所有工作示例都表明这应该有效,但我不知道为什么我的不行.我不知道要查看堆栈的哪个部分.
All of the working examples on blog posts and docs and the like show that this should work, and I don't know why mine isn't. I don't know which part of the stack to look at.
这是当我运行上面的代码时 redis-cli 监视器的样子:
Here's what the redis-cli monitor looks like when I run the code above:
1353227107.912512 [0 127.0.0.1:56759] "auth" "password"
1353227107.912719 [0 127.0.0.1:56758] "auth" "password"
1353227107.913470 [0 127.0.0.1:56759] "info"
1353227107.913639 [0 127.0.0.1:56758] "info"
如果我关闭密码、注释掉上面的 auth 行并成功运行应用程序,redis-cli 监视器显示的内容如下:
And here's what redis-cli monitor shows if I turn off the password, comment out the auth lines above, and successfully run the app:
1353227252.401667 [0 127.0.0.1:56771] "info"
1353227252.402020 [0 127.0.0.1:56770] "info"
1353227252.402131 [0 127.0.0.1:56769] "info"
1353227252.402423 [0 127.0.0.1:56768] "info"
1353227252.402611 [0 127.0.0.1:56767] "info"
1353227252.406254 [0 127.0.0.1:56770] "subscribe" "handshake"
1353227252.406287 [0 127.0.0.1:56770] "subscribe" "connect"
1353227252.406314 [0 127.0.0.1:56770] "subscribe" "open"
1353227252.406321 [0 127.0.0.1:56770] "subscribe" "join"
1353227252.406326 [0 127.0.0.1:56770] "subscribe" "leave"
1353227252.406337 [0 127.0.0.1:56770] "subscribe" "close"
1353227252.406354 [0 127.0.0.1:56770] "subscribe" "dispatch"
1353227252.406372 [0 127.0.0.1:56770] "subscribe" "disconnect"
成功(无密码)连接发出 5 个信息"命令,而我不成功(密码)的命令发出 2 个 - 然后在调用on_info_cmd"方法时死亡.
The successful (passwordless) connection makes 5 "info" commands, and my unsuccessful (passworded) command makes 2 - and then dies on a call to an "on_info_cmd" method.
有人能理解吗?感谢您提供的任何帮助.
Can anyone make sense of this? Thanks for any help you can give.
推荐答案
我通过将 redis 模块本身作为选项传递给 RedisStore 构造函数解决了这个问题.
I solved this by passing the redis module itself as an option to the RedisStore constructor.
io.set('store', new RedisStore({redis: redis, redisPub: redisClient, redisSub: redisSubscriber, redisClient: redisClient }));
这对于客户端对象通过 instanceof RedisClient
测试并且在没有密码的情况下不会重新初始化是必要的.显然,当 RedisStore
重新需要 redis 模块时,使用 createClient
方法创建的 redis 客户端是某个新类或其他东西的成员.
This was necessary for the client objects to pass the instanceof RedisClient
test and not be re-initialized without a password. Apparently, when RedisStore
re-requires the redis module, redis clients created with the createClient
method are members of some new class or something.
我通过查看某人在 socket.io 的 issue #808.
I figured this out by looking at a related issue someone was having on socket.io's issue #808.
这篇关于Node.js 和 socket.io 的 Redis 身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!