api访问权限控制

1.当用户登录时,在redis中存储对应的权限,在openresty配置要进行控制的权限,解析成一颗树

  

2.在用户访问api时,通过用户id在openresty中提取权限,和权限树进行比较。

local permission = MatchUrlTree(ngx.var.uri,ngx.req.get_method())
  if permission then
    local permission_json,err =redis_sess:get(permission_key)
    if permission_json == ngx.null or permission_json == "" then
      ngx.exit(ngx.HTTP_FORBIDDEN) 
    end
    local ok,permission_data = pcall(json.decode,permission_json)
    if not ok then 
      ngx.log(ngx.ERR, "json decode error")
      ngx.exit(ngx.OK)
    end
    if not permission_data[permission] then
      ngx.exit(ngx.HTTP_FORBIDDEN) 
    end
  end
end
作者:严彦彪 原创作品转载请注明出处
原文地址:https://www.cnblogs.com/yanbiao/p/9870419.html