用lua实现登陆超时拦截
程序员文章站
2022-07-16 13:20:01
...
local ngx = require("ngx")
local resty_redis = require("resty.redis")
local resty_cookie = require("resty.cookie")
local cjson = require("cjson")
local notFilterUrlMap={
['/mimosaui/login.html'] ="login"--not check url
}
local redis=resty_redis:new()
redis:set_timeout(1000)
ok,err = redis:connect("192.168.23.21","6379")
if not ok then
ngx.log(ngx.ERR,"failed to connect to redis:",err)
end
-- auth
ok, err = redis:get_reused_times()
if 0 == ok then
local ok, err = redis:auth("goooUO4056")
if not ok then
ngx.say("failed to authenticate: ", err)
return
end
elseif err then
ngx.say("failed to get_reused_times: ", err)
end
local login_url='/mimosaui/redirect.html';
local uid='';
local function getuid()
local cookie=resty_cookie:new()
uid=cookie:get("SESSION")
if not uid then
return nil
end
ngx.log(ngx.ERR,"uid is =====",uid)
local res,err=redis:exists("spring:session:sessions:expires:"..uid)
ngx.log(ngx.ERR,"hget==",res)
if res == 0 then
ngx.log(ngx.ERR,"uid is missing,return nil")
return nil;
else
return uid
end
return nil
end
local _url=ngx.var.uri
ngx.log(ngx.ERR,"url=:",_url)
_url=notFilterUrlMap[_url]
if not _url then
uid= getuid();
end
redis:set_keepalive(10000,100)
ngx.log(ngx.ERR,tostring(uid))
if uid == nil then
ngx.log(ngx.ERR,"uid is missing,redirect",login_url)
--ngx.location.capture("/operate/admin/info")
-- ngx.req.set_uri(login_url,true)
ngx.redirect(login_url)
-- local result = {errorCode=10002,errorMessage=""}
-- ngx.header.content_type="application/json"
-- ngx.say(cjson.encode(result))
end
local resty_redis = require("resty.redis")
local resty_cookie = require("resty.cookie")
local cjson = require("cjson")
local notFilterUrlMap={
['/mimosaui/login.html'] ="login"--not check url
}
local redis=resty_redis:new()
redis:set_timeout(1000)
ok,err = redis:connect("192.168.23.21","6379")
if not ok then
ngx.log(ngx.ERR,"failed to connect to redis:",err)
end
-- auth
ok, err = redis:get_reused_times()
if 0 == ok then
local ok, err = redis:auth("goooUO4056")
if not ok then
ngx.say("failed to authenticate: ", err)
return
end
elseif err then
ngx.say("failed to get_reused_times: ", err)
end
local login_url='/mimosaui/redirect.html';
local uid='';
local function getuid()
local cookie=resty_cookie:new()
uid=cookie:get("SESSION")
if not uid then
return nil
end
ngx.log(ngx.ERR,"uid is =====",uid)
local res,err=redis:exists("spring:session:sessions:expires:"..uid)
ngx.log(ngx.ERR,"hget==",res)
if res == 0 then
ngx.log(ngx.ERR,"uid is missing,return nil")
return nil;
else
return uid
end
return nil
end
local _url=ngx.var.uri
ngx.log(ngx.ERR,"url=:",_url)
_url=notFilterUrlMap[_url]
if not _url then
uid= getuid();
end
redis:set_keepalive(10000,100)
ngx.log(ngx.ERR,tostring(uid))
if uid == nil then
ngx.log(ngx.ERR,"uid is missing,redirect",login_url)
--ngx.location.capture("/operate/admin/info")
-- ngx.req.set_uri(login_url,true)
ngx.redirect(login_url)
-- local result = {errorCode=10002,errorMessage=""}
-- ngx.header.content_type="application/json"
-- ngx.say(cjson.encode(result))
end