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

 

相关文章:

  • 2021-11-26
  • 2021-05-24
  • 2021-10-16
  • 2021-12-21
  • 2021-07-21
  • 2021-09-28
  • 2021-10-15
猜你喜欢
  • 2018-04-11
  • 2021-07-03
  • 2018-06-03
相关资源
相似解决方案