1
local password = json_string["password"] or "None"
local redisPassword = red:hmget(userName,"password") or None
local redisAuthtoken = red:hmget(userName,"authToken")
if (tostring(password)   ==  tostring(redisPassword))
then
    ngx.say(redisAuthtoken) 
else
    ngx.say("Error User or Service Not found 1510")
end 

密码 = 管理员 redisPassword = 管理员

我能够将两个密码都视为输出管理员,但它在 lua 代码中不匹配,并且控制总是转到其他部分。

当我这样比较时

if (tostring(password) == "admin" )

它工作正常,这意味着问题出在 redis 值上,但我在 redis 中设置了密码值 admin。

4

1 回答 1

2

我的理解是hmget返回multi-bulk reply,它的结果在一个 lua 表中,所以你应该做这样的事情:

local res, err = red:hmget(userName,"password")
local redisPassword
if res and res ~= ngx.null then redisPassword = res[1] end
ngx.say(redisPassword)

文档中:

非零 Redis “多批量回复”会导致 Lua 表包含所有组成值(如果有)。如果任何组成值是有效的 redis 错误值,那么它将是一个二元素表 {false, err}。一个 nil 多批量回复以 ngx.null 值返回。

请注意,该ngx.null值与 Luanil值不同,因此您可能需要单独检查。

另请注意,您在两个不同的地方将其None用作变量和"None"字符串,这可能不会达到您的预期。

于 2016-05-01T22:32:49.817 回答