open-resty 服务安装jwt插件

作者:程序那点事儿 日期:2023/11/16 22:07


lua-resty-jwt 插件

如果想使用Lua识别用户令牌,我们需要引入lua-resty-jwt模块,是用于 ngx_lua 和 LuaJIT 的 Lua 实现库,在该模块能实现Jwt令牌生成、Jwt令牌校验。

下载好了该依赖库lua-resty-jwt-master.zip,我们将该库文件上传到服务器上,并解压,当然,我们也可以使用opm直接安装lua-resty-jwt

解压安装方式参考上面安装lua-resty-kafka,我们现在使用opm的安装方式

  1. yum install yum-utils #安装仓库管理工具包
  2. yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo #添加仓库地址
  3. yum install openresty-resty #安装resty
  4. yum install openresty-opm #安装opm
  5. opm get SkyLothar/lua-resty-jwt #安装Jwt组件
  6. 如果已经安装好OpenResty,跳过第2、3步(建议使用这种方式,如果代码clone不下来)。

添加一个案例访问

在前面kafka的案例中,我们已经创建了lua目录,现在我们就进入到lua目录开始。

  1. vim token.lua #封装一个token.lua脚本,以便复用

    --识别令牌
    --依赖resty.jwt库
    local jwt = require "resty.jwt"

    --定义一个模块
    local jwttoken = {}

    --秘钥
    local secret = "5pil6aOO5YaN576O5Lmf5q+U5LiN5LiK5bCP6ZuF55qE56yR"

    function jwttoken.check(auth_header)
    --响应信息
    local response = {}
    --令牌是否为空
    if auth_header == nil then
    response["code"] = 401
    response["message"] = "请登录"
    return response
    end
    --判断令牌格式 Bearer ,正确还要去掉Bearer
    local ,,token = string.find(auth_header, "Bearer%s+(.+)")
    if token == nil then
    response["code"] = 402
    response["message"] = "令牌无效"
    return response
    end

    复制代码
         --校验令牌是否正确
         local jwt_obj = jwt:verify(secret,token)
         if jwt_obj.verified == false then
                 response["code"] = 403
                 response["message"] = "令牌无效"
                 return response
         end
         --响应正确信息
         response["code"] = 200
         response["message"] = "令牌有效"
         response["body"] = jwt_obj
         return response

    end
    return jwttoken

  2. vim auth_verify.lua #创建auth_verify.lua脚本(对外访问)

    --判断测试令牌是否有效
    --设置编码utf8
    ngx.header.content_type="application/json;charset=utf8"

    --引入cjson库
    local cjson = require "cjson"
    --引入token库
    local jwttoken = require "token"

    --获取令牌(请求头中获取令牌)
    local auth_header = ngx.var.http_Authorization

    --调用token的check方法
    local result = jwttoken.check(auth_header)

    --将结果响应给客户端
    ngx.say(cjson.encode(result))

  3. vim ../conf/nginx.conf #添加配置

    复制代码
     #lua插件位置
     lua_package_path "/usr/local/openresty/nginx/lua/?.lua;/usr/local/openresty/modules/kafka/lib/?.lua;;";
     server {
         listen       80;
         server_name  localhost;
    
    
         location /token {
             content_by_lua_file /usr/local/openresty/nginx/lua/auth_verify.lua;
         }

本项目中自己写的lua脚本在/usr/local/openresty/nginx/lua/目录下,因为我们在该目录下写了两个lua脚本:auth_verify.lua,token.lua,在auth_verify.lua中引入了token.lua模块,所以别忘了在nginx.conf文件中添加如下配置,不然会报如下错误:

  1. nginx -s reload #重启ningx
  2. http://192.168.10.100/token #访问测试,结果如下
相关推荐
春日见5 分钟前
端到端自动驾驶综述
linux·人工智能·算法·机器学习·自动驾驶
Trouvaille ~20 分钟前
【项目篇】从零手写高并发服务器(六):EventLoop事件循环——Reactor的心脏
linux·运维·服务器·c++·高并发·epoll·reactor模式
bai_lan_ya20 分钟前
linux -- 文件IO
linux·服务器
林鸿群29 分钟前
Ubuntu 26.04 本地安装 GitLab CE 完整教程(非 Docker 方式)
linux·ubuntu·gitlab·私有部署·代码托管·ubuntu 26.04·omnibus
qq_172805591 小时前
腾讯云WordPress遭遇Nginx 502问题排查与解决方案
nginx·腾讯云
勇闯逆流河1 小时前
【Linux】Linux进程概念(进程优先级,进程切换详解)
linux·运维·服务器
老师好,我是刘同学1 小时前
30个核心Linux命令速查手册
linux
fsj2009yx1 小时前
如何把无公网的求生之路2服务器借助VPS转发注册到steam master列表中
linux·wireguard·求生之路2
慵懒的猫mi1 小时前
deepin UOS AI 助手接入飞书(Feishu)配置指南
linux·人工智能·ai·gpt-3·飞书·文心一言·deepin
Jiozg1 小时前
ES安装到linux(ubuntu)
linux·ubuntu·elasticsearch