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

在CentOS7环境中,实现使用openresty配置文件,达到jwt指定用户userid不能访问的效果

#在CentOS7环境中,实现使用openresty配置文件,达到jwt指定用户userid不能访问的效果。

首先,你需要安装 OpenResty 和 JWT 组件:

  1. 安装 OpenResty

参考 OpenResty 的官方安装文档,在终端执行如下命令:

$ sudo yum install yum-utils
$ sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
$ sudo yum install openresty
  1. 安装 LuaRocks 和 JWT
$ sudo yum install epel-release -y
$ sudo yum install lua-nginx* -y
$ sudo yum install curl -y
$ sudo yum install wget -y
$ sudo yum localinstall https://luarocks.org/releases/luarocks-3.5.0.tar.gz
$ sudo luarocks install lua-resty-jwt

接下来,你可以在 OpenResty 配置文件中实现需求:

  1. 在 nginx.conf 文件中,添加 http 块:
http {
...
}
  1. http 块中添加以下内容:
# 在 server 块中定义变量
lua_shared_dict jwt_dict 10m;
init_by_lua_block {local jwt = require "resty.jwt"local jwt_secret = "your-jwt-secret"-- 将 jwt 验证结果保存到共享内存中function save_jwt_result(jwt_result)local dict = ngx.shared.jwt_dictlocal key = ngx.var.http_authorizationlocal expires = 60 * 60 -- 设置过期时间为一小时if jwt_result.valid thendict:set(key, jwt_result.payload, expires)elsedict:set(key, 0, expires)endend-- 验证 jwtfunction validate_jwt()local auth_header = ngx.var.http_authorizationif not auth_header thenngx.exit(401)endlocal jwt_token = string.match(auth_header, "^Bearer%s+(.+)$")if not jwt_token thenngx.exit(401)endlocal jwt_obj = jwt:verify(jwt_secret, jwt_token)save_jwt_result(jwt_obj)if not jwt_obj.valid thenngx.exit(401)endend
}# 定义 server 块
server {listen 7000;server_name localhost;# 声明 location 块,匹配 /api/ 开头的请求location ^~ /api/ {# 验证 jwtaccess_by_lua_block {validate_jwt()-- 查询 PostgreSQL 数据库,获取 accountblack 表-- 如果用户 userid 被加入了黑名单,返回 403 状态码,否则继续执行local pgsql = require("resty.postgres")local pg = pgsql:new()pg:set_timeout(1000)-- 连接 PostgreSQL 数据库local ok, err = pg:connect{host = "your-postgres-db-host",port = 5432,database = "your-postgres-db-name",user = "your-postgres-db-username",password = "your-postgres-db-password"}if not ok thenngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)end-- 查询用户是否在黑名单中local account_black_sql = string.format("SELECT accountid FROM accountblack WHERE userid = %s", ngx.var.jwt_payload.sub)local account_black_result, err = pg:query(account_black_sql)if not account_black_result thenngx.log(ngx.ERR, "Failed to query accountblack: ", err)ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)endif account_black_result[1] ~= nil thenngx_exit(ngx.HTTP_FORBIDDEN)end}# 转发请求proxy_pass http://172.16.01.6:8029/;}
}
  1. 其中,save_jwt_result(jwt_result) 函数用于将 jwt 验证结果存储到共享内存中,validate_jwt() 函数用于验证 jwt,access_by_lua_block 即 access 阶段执行的 Lua 代码块。在 access_by_lua_block 中,我们查询 PostgreSQL 数据库,获取 accountblack 表,如果用户 userid 在黑名单中,返回 403 状态码,否则继续执行,并将请求转发到 http://172.16.01.6:8029/

当用户请求 /api/ 接口时,将会首先执行 validate_jwt() 函数,验证 jwt 是否有效。如果 jwt 有效,我们将会查询 PostgreSQL 数据库,检查用户 userid 是否在黑名单表中。如果用户在黑名单中,请求将会被拒绝并返回 403 状态码;否则请求将会被转发到指定地址,并且在转发过程中会自动添加上验证后的 jwt 信息。

http://www.lryc.cn/news/92532.html

相关文章:

  • SpringBoot 源码分析初始化应用上下文(1)-createApplicationContext
  • STM32队列
  • 探索Beyond Compare:让文件比较和管理变得简单高效
  • 动态网站Servelt基础
  • Docker 网络
  • Tomcat的优化
  • 一个问题来对比文心一言和chatgpt
  • 防雪崩利器之Hystrix
  • 机器学习复习(上)
  • node笔记_express结合formidable实现前后端的文件上传
  • CKA 09_Kubernetes工作负载与调度 资源调度 三类QoS request 资源需求 limit 资源限额
  • 【pytorch】维度变换
  • vue3中的nextTick()
  • 高效学习传感器|霍尔式传感器
  • 2023年前端面试高频考点HTML5+CSS3
  • 企业开源测试项目实战(附全套实战项目教程+视频+源码)
  • 信创办公–基于WPS的EXCEL最佳实践系列 (创建表格)
  • 四、HAL_驱动机械按键
  • 机器学习实战六步法之数据收集方法(四)
  • 神经网络:CNN中的filter,kernel_size,strides,padding对输出形状的影响
  • Spring Boot集成Redisson布隆过滤器案例
  • 使用 VSCode SSH 公网远程连接本地服务器开发 - cpolar内网穿透
  • portraiture宿主插件最新v4中文版本下载及使用教程
  • 一. ATR技术指标的定义与运用
  • linux find帮助文档
  • 搜索与图论(acwing算法基础)
  • 【数据结构】何为数据结构。
  • 【P57】JMeter 保存响应到文件(Save Responses to a file)
  • Visual Studio 2022 v17.6 正式发布
  • std::chrono时间处理