前言
因為不涉及到數據庫和其它資源的依賴,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配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
-- 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配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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; } } } |
到此這篇關于使用nginx和lua進行jwt校驗介紹的文章就介紹到這了,更多相關nginx和lua進行jwt校驗內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/jiangshanwe/article/details/121956714