码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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/;
        }

  • 相关阅读:
    O2O优惠券预测
    Apache SeaTunnel Zeta、Flink、Spark 怎么选?底层原理 + 实战对比一次讲透
    vue3源码分析——手写diff算法
    关于京东购物车中一个店铺加购商品过多时,店铺名称不被划走的实现
    Unity开发者——编辑器技巧
    Unity中Shader反射环境
    PCB电路板去耦电容配置原则有哪些?
    亚商投资顾问 早餐FM/12022023年开展第五次全国经济普查
    Vue源码总结
    Linux —用户和组
  • 原文地址:https://blog.csdn.net/ClaireCheney/article/details/127111570
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号