網站首頁 編程語言 正文
前言
因為不涉及到數據庫和其它資源的依賴,jwt本身也是無狀態的。因此鑒權服務沒有再基于Java或者其它語言來做。而是使用lua腳本對nginx做了一個增強:使用lua腳本來校驗token是否有效,無效直接返回401,有效則原樣轉發。
Lua腳本
這里的secret我遇到了很大的坑。一開始直接從Java后端項目中復制了密鑰出來,但是一直提示signature mismatch:
,后來發現后端應用中使用base64decode相關方法,在Lua腳本中增加了ngx.decode_base64(secret)
處理secret后解決問題。其實到這里還沒有解決問題,在后端debug代碼的時候,發現后端密鑰被decode的結果是一串亂碼,為了避免亂碼的問題,通過https://www.base64encode.org/重新生成secret才最終解決了問題。
如果你的項目中也遇到了這個signature mismatch:
錯誤,需要排查一下后端在生成token的時候,是否有對secret進行decode或者其它處理,在lua腳本中也要進行相應的處理。
nignx.conf配置
-- nginx-jwt.lua local cjson = require "cjson" local jwt = require "resty.jwt" --your secret local secret = "yoursecrethere" --無需鑒權api清單 local no_need_token_api_list = {'/api/register', '/api/login'} local function ignore_url (val) for index, value in ipairs(no_need_token_api_list) do if (value == val) then return true end end return false end local M = {} function M.auth() if ignore_url(ngx.var.request_uri) then return else end -- require Authorization request header local auth_header = ngx.var.http_Authorization if auth_header == nil then ngx.log(ngx.WARN, "No Authorization header") ngx.exit(ngx.HTTP_UNAUTHORIZED) end -- require Bearer token local _, _, token = string.find(auth_header, "Bearer%s+(.+)") if token == nil then ngx.log(ngx.ERR, "Missing token") ngx.exit(ngx.HTTP_UNAUTHORIZED) end --decode_base64和后端保持一致 local jwt_obj = jwt:verify(ngx.decode_base64(secret), token) if jwt_obj.verified == false then ngx.log(ngx.ERR, "Invalid token: ".. jwt_obj.reason) ngx.status = ngx.HTTP_UNAUTHORIZED ngx.say(cjson.encode(jwt_obj)) ngx.header.content_type = "application/json; charset=utf-8" ngx.exit(ngx.HTTP_UNAUTHORIZED) end end return M
Dockerfile配置
worker_processes 1; events { worker_connections 1024; } http { lua_package_path "/opt/lua-resty-jwt/lib/?.lua;;"; upstream backend { server 192.168.1.1:8080; } access_log /logs/nginx_access.log; error_log /logs/nginx_error.log; server { listen 80; #后端api接口代理 location /api/ { access_by_lua_block { local obj = require('nginx-jwt') obj.auth() } proxy_pass http://backend; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
原文鏈接:https://blog.csdn.net/jiangshanwe/article/details/121956714
相關推薦
- 2022-03-20 android自定義對話框實例代碼_Android
- 2022-06-16 C#實現優先隊列和堆排序_C#教程
- 2022-06-22 關于Metalama使用Fabric操作項目或命名空間的問題_實用技巧
- 2022-07-07 Python編寫運維進程文件目錄操作實用腳本示例_python
- 2022-09-17 C++中cin的返回值問題_C 語言
- 2022-07-11 python利用多線程+隊列技術爬取中介網互聯網網站排行榜_python
- 2023-01-17 python中終止協程和異常處理方式_python
- 2022-04-12 Python?設計模式行為型訪問者模式_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支