当前位置: 首页 > 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
http://www.xdnf.cn/news/590959.html

相关文章:

  • upload-labs通关笔记-第18关文件上传之条件竞争
  • 数据结构篇--二项队列
  • linux服务器查看端口是否被占用
  • 5月22日复盘-YOLOV5
  • SQLServer与MySQL数据迁移案例解析
  • fscan教程1-存活主机探测与端口扫描
  • Android 添加系统服务的完整流程
  • JavaScript【9】ES语法
  • 阿里云 Serverless 助力海牙湾构建弹性、高效、智能的 AI 数字化平台
  • 新手到资深的Java开发编码规范
  • Python爬虫实战:研究Crawley 框架相关技术
  • 【Java Web】1.Maven
  • Docker常用命令介绍
  • upload-labs靶场通关详解:第14关
  • PyQt学习系列01-框架概述与基础环境搭建
  • 25.5.22学习总结
  • MCP Server Tool 开发学习文档
  • 国产数据库:tidb专题
  • 微信小程序 隐私协议弹窗授权
  • Git分支的强制回滚
  • 辽宁省工程系列信息通信管理专业职称评审标准
  • 国芯思辰| 高精度线性霍尔传感器AH693在角度位置传感器中的应用
  • 【机器学习】欠拟合、过拟合和正则化
  • ARM Linux远程调试
  • day 33简单的神经网络
  • Linux `wc` 命令深度解析与高阶应用指南
  • 计算机网络——Session、Cookie 和 Token
  • Bert预训练任务-MLM/NSP
  • 数仓SQL投影介绍
  • 小米2025年校招笔试真题手撕(一)