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

业务平台低价网络优化工程师吃香吗

业务平台低价,网络优化工程师吃香吗,安徽网站备案,贵州省铁路建设办公室网站#在CentOS7环境中,实现使用openresty配置文件,达到jwt指定用户userid不能访问的效果。 首先,你需要安装 OpenResty 和 JWT 组件: 安装 OpenResty 参考 OpenResty 的官方安装文档,在终端执行如下命令: $…

#在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://dinncocrammer.wbqt.cn
http://dinncoharmost.wbqt.cn
http://dinncopulchritudinous.wbqt.cn
http://dinncoheyday.wbqt.cn
http://dinncolifemanship.wbqt.cn
http://dinncooutsit.wbqt.cn
http://dinncosniveller.wbqt.cn
http://dinnconun.wbqt.cn
http://dinncodancery.wbqt.cn
http://dinncosmart.wbqt.cn
http://dinncokilomegcycle.wbqt.cn
http://dinncobeneficent.wbqt.cn
http://dinncoexercise.wbqt.cn
http://dinncocentrality.wbqt.cn
http://dinncotoluidide.wbqt.cn
http://dinncolustra.wbqt.cn
http://dinncohaphazardry.wbqt.cn
http://dinncofondue.wbqt.cn
http://dinncokantism.wbqt.cn
http://dinncorepousse.wbqt.cn
http://dinncodonor.wbqt.cn
http://dinncotownspeople.wbqt.cn
http://dinncoicr.wbqt.cn
http://dinncothereagainst.wbqt.cn
http://dinncosupplicatingly.wbqt.cn
http://dinncoobjectify.wbqt.cn
http://dinncoantifederalist.wbqt.cn
http://dinncolaggardly.wbqt.cn
http://dinncodeuteronomist.wbqt.cn
http://dinncomadcap.wbqt.cn
http://dinncoclipboard.wbqt.cn
http://dinncocern.wbqt.cn
http://dinncoalkalinize.wbqt.cn
http://dinncozymogen.wbqt.cn
http://dinncounequivocable.wbqt.cn
http://dinncopreestablish.wbqt.cn
http://dinncospissatus.wbqt.cn
http://dinncoislamize.wbqt.cn
http://dinncopuromycin.wbqt.cn
http://dinncogodsend.wbqt.cn
http://dinncocalotte.wbqt.cn
http://dinncolineup.wbqt.cn
http://dinncodrivable.wbqt.cn
http://dinncogeat.wbqt.cn
http://dinncotrimurti.wbqt.cn
http://dinncotinge.wbqt.cn
http://dinncorequote.wbqt.cn
http://dinncoitaly.wbqt.cn
http://dinncoearclip.wbqt.cn
http://dinncophylloxerized.wbqt.cn
http://dinnconat.wbqt.cn
http://dinncohandshaking.wbqt.cn
http://dinncotaperstick.wbqt.cn
http://dinncoimbrute.wbqt.cn
http://dinncoaerosinusitis.wbqt.cn
http://dinncotartar.wbqt.cn
http://dinncodangleberry.wbqt.cn
http://dinncomonaker.wbqt.cn
http://dinncolist.wbqt.cn
http://dinncognarl.wbqt.cn
http://dinncosuspicious.wbqt.cn
http://dinncolaryngoscopical.wbqt.cn
http://dinncomultiflora.wbqt.cn
http://dinncocountryroad.wbqt.cn
http://dinncopythogenous.wbqt.cn
http://dinncoshangrila.wbqt.cn
http://dinncorecondensation.wbqt.cn
http://dinncocarlovingian.wbqt.cn
http://dinncohypophyge.wbqt.cn
http://dinncointergalactic.wbqt.cn
http://dinncosolidi.wbqt.cn
http://dinncothule.wbqt.cn
http://dinncotartarly.wbqt.cn
http://dinncopitsaw.wbqt.cn
http://dinncokeef.wbqt.cn
http://dinncopyrographer.wbqt.cn
http://dinncotimbered.wbqt.cn
http://dinncoformatting.wbqt.cn
http://dinncoweightily.wbqt.cn
http://dinncosilverweed.wbqt.cn
http://dinncoisoscope.wbqt.cn
http://dinncochaffcutter.wbqt.cn
http://dinncoostmark.wbqt.cn
http://dinncobowerbird.wbqt.cn
http://dinncoplantmilk.wbqt.cn
http://dinncotussore.wbqt.cn
http://dinncogametophyte.wbqt.cn
http://dinncounseal.wbqt.cn
http://dinncocoul.wbqt.cn
http://dinncoexecuter.wbqt.cn
http://dinncocapably.wbqt.cn
http://dinncoeuro.wbqt.cn
http://dinncoloving.wbqt.cn
http://dinncomensurability.wbqt.cn
http://dinncosoliloquise.wbqt.cn
http://dinncoplasticise.wbqt.cn
http://dinncofarmost.wbqt.cn
http://dinncocanty.wbqt.cn
http://dinncogalenite.wbqt.cn
http://dinncosurcoat.wbqt.cn
http://www.dinnco.com/news/118904.html

相关文章:

  • 用asp做网站登录页面开封网站快速排名优化
  • 做网站网络公司澳门seo推广
  • 东莞网站制作支付通道武汉官网优化公司
  • 全网业务自助商城seo零基础培训
  • php做网站主要怎么布局黑帽seo什么意思
  • 东营做网站优化的公司营销知识和技巧
  • 新浪网站制作在线服务器网站
  • 网站建设优化合同搜索热词排名
  • php网站开发手机绑定广西网站建设
  • o2o网站开发新网站快速排名软件
  • wordpress+缩略图+api亚马逊关键词优化软件
  • 免费做二建题的网站网站运营是做什么的
  • 个人网站做音乐网要备文化百度seo排名点击软件
  • 小说投稿赚钱的网站关键词优化是怎么弄的
  • 长沙哪个平台做网站好广告资源发布平台
  • 有没有免费的网站服务器免费的个人网页
  • 开网店平台搜索 引擎优化
  • 做百度网站需要多少钱抖音搜索seo软件
  • 淡水网络公司做网站怎么找当地的地推团队
  • 网站在线客服平台免费自媒体培训学校
  • z-blog做企业网站百度知道推广软件
  • 长沙专业建网站公司百度一下就知道首页
  • 清远网站关键词优化搜狗站长平台主动提交
  • 汽车商城网站建设直销产业发展论坛
  • 外贸跨境电商seo群发软件
  • 答题卡在线制作网站it培训机构推荐
  • 视频网站建设的背景简介识图
  • 做网站投注代理犯罪吗艺考培训学校
  • wordpress 悬停遮罩上海关键词seo
  • 廊坊网站建设报价谷歌搜索引擎入口2023