Skip to content

Commit defe93c

Browse files
committed
fix: 兼容jwt payload格式
1 parent 7bdb5a3 commit defe93c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ services:
1414
- ${PWD}:/data
1515
- ${PWD}/lib:/usr/local/openresty/nginx/lua
1616
- ${PWD}/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf
17-
- ${PWD}/sites.d:/usr/local/openresty/nginx/conf/sites.d
1817
- ${PWD}/.opmrc:/root/.opmrc
1918
privileged: true
2019
ports:

lib/resty/waf/lib/comm.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ function _M.get_user_id()
2828
if auth_header then
2929
_, _, jwt_token = string.find(auth_header, "Bearer%s+(.+)")
3030
end
31+
local user_id = 0
3132
local jwt_obj = (jwt_token ~= nil and jwt:load_jwt(jwt_token) or nil)
32-
return jwt_obj ~= nil and jwt_obj.payload ~= nil and jwt_obj.payload.sub or 0
33+
if jwt_obj ~= nil and jwt_obj.payload ~= nil and type(jwt_obj.payload) == 'number' then
34+
user_id = jwt_obj.payload
35+
elseif jwt_obj ~= nil and jwt_obj.payload ~= nil and jwt_obj.payload.sub ~= nil then
36+
user_id = jwt_obj.payload.sub
37+
else
38+
user_id = 0
39+
end
40+
return user_id
3341
end
3442

3543
function _M.error(err)

nginx.conf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ http {
121121
_, _, jwt_token = string.find(auth_header, "Bearer%s+(.+)")
122122
end
123123
local jwt_obj = (jwt_token ~= nil and jwt:load_jwt(jwt_token) or nil)
124-
ngx.var.user_id = jwt_obj ~= nil and jwt_obj.payload ~= nil and jwt_obj.payload.sub or 0
124+
if jwt_obj ~= nil and jwt_obj.payload ~= nil and type(jwt_obj.payload) == 'number' then
125+
ngx.var.user_id = jwt_obj.payload
126+
elseif jwt_obj ~= nil and jwt_obj.payload ~= nil and jwt_obj.payload.sub ~= nil then
127+
ngx.var.user_id = jwt_obj.payload.sub
128+
else
129+
ngx.var.user_id = 0
130+
end
125131
}
126132
}
127133
}

0 commit comments

Comments
 (0)