From 6e8d06dad3e2aca6e08e0bd933eebe6525ef275e Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Tue, 18 Nov 2025 15:13:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=B7=A8=E7=AB=99=EF=BC=8C=E5=B9=B6=E5=AF=B9=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E8=AF=B7=E6=B1=82=E7=9A=84options=E9=A2=84=E6=A3=80?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E8=BF=87=E6=BB=A4=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E6=8A=A5=E9=94=99=E6=97=A0=E6=B3=95=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/nginx.conf | 21 ++++++++++++--------- src/auth/jwt-auth.lua | 6 ++++++ src/service/system/user.lua | 2 +- src/util/response.lua | 8 ++++++-- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/conf/nginx.conf b/conf/nginx.conf index bbb2128..365d596 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -13,7 +13,7 @@ http { client_max_body_size 1024M; #允许最大100k的请求体 client_body_buffer_size 1024M; #设置缓冲区大小 - #lua_code_cache off; #关闭代码缓存,修改lua脚本不需要重启 + lua_code_cache on; #代码缓存 lua_package_path '$prefix/src/?/?.lua;$prefix/src/?.lua;/home/frankly/work/AuthPlatform/src/?/?.lua;/home/frankly/work/AuthPlatform/src/?.lua;;'; lua_package_cpath '$prefix/src/share/lib/?.so;/home/frankly/work/AuthPlatform/src/share/lib/?.so;;'; @@ -52,16 +52,19 @@ http { ## 应用路径 todo 路径问题 set $APP_PATH '/home/frankly/work/AuthPlatform'; - #访问时允许跨域处理 + # 全局 CORS 配置 访问时允许跨域处理 access_by_lua_block { - ngx.header["Access-Control-Allow-Origin"] = "*"; - ngx.header["Access-Control-Allow-Methods"] = "GET, POST, DELETE, PUT"; - ngx.header["Access-Control-Allow-Headers"] = "*"; - ngx.header["Access-Control-Max-Age"] = 1728000; - ngx.header["Access-Control-Expose-Headers"] = "*"; + ngx.header["Access-Control-Allow-Origin"] = "*" -- 允许所有源,或者指定特定的源,例如 "http://example.com" + --ngx.header["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS" + ngx.header["Access-Control-Allow-Methods"] = "*" + --ngx.header["Access-Control-Allow-Headers"] = "Content-Type, Authorization" + ngx.header["Access-Control-Allow-Headers"] = "*" + ngx.header["Access-Control-Max-Age"] = 1728000 -- 预检结果缓存时间,单位秒 + print("request_method:", ngx.var.request_method) if ngx.var.request_method == "OPTIONS" then - ngx.status = 204 - ngx.exit(ngx.OK) + ngx.header["Content-Length"] = 0 -- 对于 OPTIONS 请求,内容长度为0 + ngx.status = 204 -- No Content,适用于 OPTIONS 请求的响应状态码 + ngx.exit(ngx.OK) -- 结束请求处理 end } diff --git a/src/auth/jwt-auth.lua b/src/auth/jwt-auth.lua index 5fccf62..064357f 100644 --- a/src/auth/jwt-auth.lua +++ b/src/auth/jwt-auth.lua @@ -11,6 +11,12 @@ local schema = { }, required = {"Authorization"} } +--对域检方法类型进行直接返回 +if ngx.var.request_method == "OPTIONS" then + ngx.status = 204 -- No Content,适用于 OPTIONS 请求的响应状态码 + ngx.exit(ngx.OK) -- 结束请求处理 +end + --获取用户认证数据信息 local auth_header = ngx.var.http_Authorization diff --git a/src/service/system/user.lua b/src/service/system/user.lua index 98138e0..ef6f5b7 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -72,7 +72,7 @@ function _M.getSystemUser(m) local code, ret = userDao.getSystemUser(m.id) local state = status.SUCCESS if code ~= 0 then state = status.DATA_IS_WRONG end - resp: response(state, ret) + resp:response(state, ret) end --根据用户id获取用户信息 diff --git a/src/util/response.lua b/src/util/response.lua index 3e6512c..30a1796 100644 --- a/src/util/response.lua +++ b/src/util/response.lua @@ -13,6 +13,7 @@ function _M:json(state, message, data, http_status) msg = status.message end local response = { code = code, msg = msg, result = data, timestamp = ngx.time() } + print("response:", cjson.encode(response)) return { code = response_status, headers = { content_type = 'application/json; charset=UTF-8' }, @@ -27,6 +28,7 @@ function _M:json(state, data, http_status) local msg = status.message local response_status = http_status or ngx.HTTP_OK local response = { code = code, msg = msg, result = data,timestamp = ngx.time() } + --print("response:", cjson.encode(response)) return { code = response_status, headers = { content_type = 'application/json; charset=UTF-8' }, @@ -59,14 +61,16 @@ function _M:send(response) ngx.header[name] = value end end + --print("send data:", response.body) if response.body ~= nil then + --print("send data:", response.body) ngx.say(response.body) end end function _M:response(state, result) - local response = self:json(state, result) - self:send(response) + local resp = self:json(state, result) + self:send(resp) end return _M