当前位置: 首页 > news >正文

安装openresty使用nginx+lua,openresty使用jwt解密

yum install -y epel-release
yum update
yum search openresty  # 查看是否有可用包
yum install -y openresty启动systemctl start openresty验证服务状态systemctl status openresty设置开机自启systemctl enable openrestysystemctl stop openresty      # 停止服务
systemctl restart openresty   # 重启服务
systemctl reload openresty    # 重载配置(无需重启服务)

使用
nginx配置文件

location / {root   html;index  index.html index.htm;//直接使用//content_by_lua_block {//    ngx.say("Hello, OpenResty!")//	  ngx.var.uri获得请求地址//     ngx.say("Request URI: ", ngx.var.uri)// }//lua文件content_by_lua_file "/usr/local/openresty/nginx/conf/lua/app.lua";
}

app.lua

-- 获取请求参数
local args = ngx.req.get_uri_args()
-- 设置响应头
ngx.header.content_type = "application/json; charset=utf-8"ngx.say('{"code":2006,"data":[],"message":"请求成功"}')

使用解密

安装
yum install openresty-opm 
安装
opm get SkyLothar/lua-resty-jwt引入
local jwt = require("resty.jwt")
使用
local secret = "wbrj"  -- JWT签名密钥
local jwt_obj = jwt:verify(secret, headers["token"])

完整案例app.lua文件

-- 获取请求参数
local args = ngx.req.get_uri_args()
-- 设置响应头
ngx.header.content_type = "application/json; charset=utf-8"-- 引入JSON编码库
local cjson = require("cjson")local uri = ngx.var.uri-- 定义不需要拦截的路径数组
local whitelist = {"/main/admin/login","/main/admin/wbUrl", --App绑定域名"/main/accset/select", --获得套账数"/main/accset/select", --获得套账数"/wbrjPys/accset/select",--获得套账数"/wbrjPys/static" -- 静态文件
}
-- 检查URI是否在白名单中
local in_whitelist = false
for _, path in ipairs(whitelist) doif string.find(uri, path) thenin_whitelist = truebreakend
end
-- 如果在白名单中,直接放行
if in_whitelist thenreturn
end-- 获取所有请求头信息
local headers = ngx.req.get_headers()-- 使用Lua表构建响应数据,code是2006
local response = {code = 2001,data = {},message = "签权失败",uri = headers['token']
}
local jwt = require("resty.jwt")-- 检查请求头中是否存在token字段
if headers["token"] and headers["token"] ~= "" then-- 使用示例local secret = "dade"  -- JWT签名密钥local jwt_obj = jwt:verify(secret, headers["token"])response['valid'] = jwt_objngx.say(cjson.encode(response))
else--自动编码为JSON字符串,自动处理转义ngx.say(cjson.encode(response))
end

相关文章:

  • 第33节:迁移学习与模型微调策略
  • 在mobaxterm下面执行shell脚本报错
  • GIM发布新版本了 (附rust CLI制作brew bottle流程)
  • Security
  • 基于python,html,echart,php,mysql,在线实时监控入侵检测系统
  • 6.12.有向无环图描述表达式
  • Python实现Web请求与响应
  • Antd中Upload组件封装及使用:
  • 矩阵短剧系统:如何用1个后台管理100+小程序?技术解析与实战应用
  • CUDA加速的线性代数求解器库cuSOLVER
  • 基于系统整合的WordPress个性化配置方法深度解析:从需求分析到实现过程
  • LeetCode[222]完全二叉树的节点个数
  • 水库大坝、坝肩混凝土面板变形及岸坡位移多断面多测点安全监测新途径——变焦视觉位移监测仪
  • 【优质会议推荐】2025年遥感与航天航空国际会议(IACRSA 2025)
  • `Release`模式下 编译器优化对 gRPC 远程调用的影响 导致堆栈非法访问
  • leetcode 438. 找到字符串中所有字母异位词
  • Axure项目实战:智慧运输平台后台管理端-运单管理
  • 古诗词鉴赏代码
  • 力扣.H指数力扣.字母异位词力扣.289生命游戏力扣452.用最小数量的箭引爆气球力扣.86分隔链表力扣.轮转数组
  • vue vite textarea标签按下Shift+Enter 换行输入,只按Enter则提交的实现思路
  • 株洲手机网站建设/微信公众平台开发
  • 用dw做网站毕业设计/品牌如何做推广
  • 淘宝网站模板是什么做的/徐州seo外包
  • 如何免费做网站推广/温州网站建设
  • 石家庄高端网站建设/seo专业培训班
  • 怎样做网站分析总结/全网自媒体平台大全