docker构建openrestry + jwt

准备 lua-resty-jwt 源码 地址 GitHub - iresty/lua-resty-jwt: JWT For The Great Openresty

openresty 安装包 OpenResty® - 中文官方站

FROM centos:7
ADD openresty-1.17.8.1.tar.gz /
ADD lua-resty-jwt/ /lua-resty-jwt
RUN yum -y install perl gcc make pcre-devel openssl openssl-devel
RUN cd /openresty-1.17.8.1 && ./configure --prefix=/opt/openresty \
--with-luajit \
--add-module=/ngx_cache_purge-2.3 \
--without-http_redis2_module && make && make install && \
ln -sf /dev/stdout /opt/openresty/nginx/logs/access.log && \
ln -sf /dev/stderr /opt/openresty/nginx/logs/error.log
RUN cp /lua-resty-jwt/lib/resty/*.lua /opt/openresty/lualib/resty/ && \ 
cp /lua-resty-jwt/vendor/resty/*.lua /opt/openresty/lualib/resty/ \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
ENV LUA_PATH=/lua/?.lua;;
WORKDIR /opt/openresty/nginx/sbin
ENTRYPOINT  ["/opt/openresty/nginx/sbin/nginx","-c","/opt/openresty/nginx/conf/nginx.conf","-g","daemon off;"]

nginx配置

# 解密
location /lua {
    content_by_lua_file /lua/main.lua;
}
# 加密
location /sign {
    content_by_lua_file /lua/tokentest.lua;
}

加密lua代码

ngx.header.content_type="text/html;charset=utf-8"
local cjson = require "cjson"
local jwt = require "resty.jwt"

local jwt_token = jwt:sign(
        -- 密钥
        "lua-resty-jwt",
        {
                header={typ="JWT", alg="HS256"},
                -- 加密内容
                payload={foo="bar"}

        }
)
ngx.say(jwt_token)

解密lua代码

ngx.header.content_type="application/json;charset=utf8"

local cjson = require("cjson")
local jwt = require("resty.jwt")
-- 获取token
local auth_header = ngx.var.http_Authorization
-- 加密密钥
local secret = "5pil6aOO5YaN576O5Lmf5q+U5LiN5LiK5bCP6ZuF55qE56yR"
-- 密钥验证
local jwt_obj = jwt:verify(secret, auth_header)
-- 将jwt验证对象返回,或者编写业务逻辑
ngx.say(cjson.encode(jwt_obj))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值