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

安装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.lryc.cn/news/2385245.html

相关文章:

  • java基础(继承)
  • python 实现一个完整的基于Python的多视角三维重建系统,包含特征提取与匹配、相机位姿估计、三维重建、优化和可视化等功能
  • 行列式中某一行的元素与另一行对应元素的代数余子式乘积之和等于零
  • 【时时三省】Python 语言----字符串,列表,元组,字典常用操作异同点
  • 基于cornerstone3D的dicom影像浏览器 第二十二章 mpr + vr
  • 优启通添加自定义浏览器及EXLOAD使用技巧分享
  • MySQL:游标 cursor 句柄
  • 二、ZooKeeper 集群部署搭建
  • << C程序设计语言第2版 >> 练习1-14 打印输入中各个字符出现频度的直方图
  • 黑马点评双拦截器和Threadlocal实现原理
  • 港股IPO市场火爆 没有港卡如何参与港股打新?
  • RESTful API 在前后端交互中的作用与实践
  • Jenkins+Docker+Harbor快速部署Spring Boot项目详解
  • python打卡训练营打卡记录day35
  • 如何评价OpenRouter这样的大模型API聚合平台?
  • 恢复二叉搜索树:递归与中序遍历的智慧应用
  • 从零开始构建一个区块链应用:技术解析与实践指南
  • 5.2.4 wpf中MultiBinding的使用方法
  • 技术服务业-首套运营商网络路由5G SA测试专网搭建完成并对外提供服务
  • 仿腾讯会议——音频服务器部分
  • 大文件上传,对接阿里oss采用前端分片技术。完成对应需求!
  • 【场景分析】基于概率距离快速削减法的风光场景生成与削减方法
  • 【Java Web】3.SpringBootWeb请求响应
  • 单片机中断系统工作原理及定时器中断应用
  • LangGraph-agent-天气助手
  • 深度学习——超参数调优
  • 阿里云API RAG全流程实战:从模型调用到多模态应用的完整技术链路
  • 创建型:建造者模式
  • Jenkins集成Docker与K8S构建
  • redis缓存实战-19(使用 Pub/Sub 构建简单的聊天应用程序)