V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Recommended Services
Amazon Web Services
LeanCloud
New Relic
ClearDB
kiduu
V2EX  ›  云计算

请问一下用 openresty 和 lua 脚本实现 url 鉴权怎么不行

  •  1
     
  •   kiduu · 2023-03-29 11:09:15 +08:00 · 1080 次点击
    这是一个创建于 399 天前的主题,其中的信息可能已经有所发展或是发生改变。
    location /image {
      content_by_lua_block {
        local secret_key = "secret_key"
        local uri = ngx.var.uri
        local args = ngx.req.get_uri_args()
        local file_path = string.sub(uri, 5)
        local timestamp = args.t
        local sign = args.sign
        local expected_sign = ngx.md5(file_path .. "@" .. timestamp .. "@" .. secret_key)
        if sign == expected_sign then
          ngx.exec(uri)
        else
          ngx.exit(ngx.HTTP_FORBIDDEN)
        end
      }
    }
    

    想要的效果是域名 /文件路径?t=timestamp&sign=hash 值,hash 值为 md5(/文件路径 @时间戳 @密钥) 用 curl 测试的时候一直 403 , curl "http://域名 /image/1.jpg?t=$(date +%s)&sign=$(echo -n "/image/1.jpg@$(date +%s)@secret_key" | md5sum | cut -d ' ' -f 1)" 技术水平不到家,望大佬指点

    1 条回复    2023-03-29 11:23:32 +08:00
    proxytoworld
        1
    proxytoworld  
       2023-03-29 11:23:32 +08:00
    好像你文件路径算错了,不会 lua ,但看你代码算的路径好像不包含 /image ??但请求里的 md5 带了 /image
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2128 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 01:59 · PVG 09:59 · LAX 18:59 · JFK 21:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.