• lua脚本使用,单个及多个参数post请求


    1、脚本内容 access_token_check.lua

    1. token = ngx.req.get_headers()['token']
    2. if (token == nil or token == '') then
    3. ngx.header['Content-Type'] = 'application/json; charset=utf-8'
    4. ngx.print('{"errorCode":"401","value":"无访问权限!","data":null}')
    5. ngx.exit(ngx.OK)
    6. end
    7. res = ngx.location.capture(
    8. '/gateway/api/gateway/checkToken',
    9. {
    10. method = ngx.HTTP_POST,
    11. --单个固定参数
    12. body = '{"channelId":"1"}'
    13. }
    14. )
    15. if (res ~= nil
    16. and res.status ~= nil
    17. and res.status == ngx.HTTP_OK) then
    18. --放入头部
    19. ngx.req.set_header('userId', res.header['userId'])
    20. ngx.req.set_header('channelId', res.header['channelId'])
    21. ngx.req.set_header('platform', res.header['platform'])
    22. --获取请求的URI
    23. local request_uri = ngx.var.request_uri
    24. --日志打印
    25. ngx.log(ngx.ERR, request_uri)
    26. ngx.log(ngx.ERR,"-----------------------------------------")
    27. ngx.header['Content-Type'] = 'application/json; charset=utf-8'
    28. ngx.log(ngx.ERR, res.body)
    29. ngx.log(ngx.ERR, res.status)
    30. check_res = ngx.location.capture(
    31. '/gateway-login/api/checkRolePermission',
    32. {
    33. method = ngx.HTTP_POST,
    34. --多个动态参数
    35. body = '{"userId":"' .. res.header['userId'] .. '","requestUri":"' .. ngx.var.request_uri .. '"}'
    36. }
    37. )
    38. if (res ~= nil
    39. and res.status ~= nil
    40. and res.status ~= ngx.HTTP_OK) then
    41. ngx.print('{"errorCode":"401","value":"权限不足!","data":null}')
    42. ngx.exit(ngx.OK)
    43. end
    44. else
    45. ngx.header['Content-Type'] = 'application/json; charset=utf-8'
    46. ngx.print('{"errorCode":"401","value":"无访问权限!","data":null}')
    47. ngx.exit(ngx.OK)
    48. end

    2、使用 

    location /gateway{
           access_by_lua_file /usr/local/openresty/lualib/access_token_check.lua;
           proxy_pass http://gateway/;
        }

  • 相关阅读:
    开源办公OA平台教程:SmartBI集成版快速部署及使用(O2OA容器化部署)
    Web of Science怎么用有哪些功能
    【软考】文件的组织结构
    全屏-多语言-tab页-主题切换
    创建ffmpeg vs2019工程
    如何运用并行编程Parallel提升任务执行效率
    玩转Vue3全家桶03丨新特性:初探Vue3新特性
    集合注入实例(八)
    break pad源码编译--参考大佬博客的总结
    OpenSSL生成自签名证书
  • 原文地址:https://blog.csdn.net/ClaireCheney/article/details/127111570