Recommended Services
Amazon Web Services
LeanCloud
New Relic
ClearDB
kiduu
V2EX  ›  云计算

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

  •  1
     
  •   kiduu · Mar 29, 2023 · 1692 views
    This topic created in 1149 days ago, the information mentioned may be changed or developed.
    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 replies    2023-03-29 11:23:32 +08:00
    proxytoworld
        1
    proxytoworld  
       Mar 29, 2023
    好像你文件路径算错了,不会 lua ,但看你代码算的路径好像不包含 /image ??但请求里的 md5 带了 /image
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5385 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 01:28 · PVG 09:28 · LAX 18:28 · JFK 21:28
    ♥ Do have faith in what you're doing.