• 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/;
        }

  • 相关阅读:
    物联网安防-园区周界安防技术实现
    SpringMvc请求原理流程
    Spring Boot 自定义starter
    JPA的注解@Field指定为Keyword失败,导致查询不到数据
    Linux中sudo命令的添加和操作
    Flask 学习-48.Flask-RESTX 使用api.model() 模型工厂
    05_Docker-Compose
    HTML做一个节日页面【六一儿童节】纯HTML代码
    Vue3 SFC 和 TSX 方式自定义组件实现 v-model
    Android5.1 文件AES加密
  • 原文地址:https://blog.csdn.net/ClaireCheney/article/details/127111570