lua 实现请求签名验证

[b]签名类[/b]

--第三方类库
dofile("/usr/local/nginx/lua/resty/sha1.lua");
--调试日志
local function log(msg)
local file = io.open('/tmp/test', 'a+')
io.output(file)
io.write('==========='..msg.."\n")
io.close(file)
end

local Auth = {}
Auth.__index = Auth

function Auth:new()
local self = {}
setmetatable(self, Auth)
return self
end

function Auth:check(sign)
if sign ~= nil and sign:find("|") ~= nil then
local pos = sign:find("|")
hmac = sign:sub(pos+1)
expire = sign:sub(0, pos-1)

--log(expire)
--sha1 加密key,可以自行约定
if hmac_sha1("sha1", expire) == hmac and tonumber(expire) >= os.time() then
return true;
end
end

return false
end

return Auth



[b]在nginx中调用[/b]
access_by_lua_file '/usr/local/nginx/lua/access.lua';


local sign = ngx.var.arg_sign --这个签名可以根据需要传递
local Auth = require "resty.auth"
local auth = Auth:new();
if auth:check(sign) then
ngx.exec("/inline") --目前接口
else
ngx.status = 403
ngx.say("forbidden")
ngx.exit(ngx.HTTP_OK)
end



参考:
http://www.stavros.io/posts/writing-an-nginx-authentication-module-in-lua/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值