From e8fe6be8de17e6fb034184790e715bbc7481bd70 Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Wed, 12 Nov 2025 14:41:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E8=BF=9B?= =?UTF-8?q?=E8=A1=8Cjson=E9=AA=8C=E8=AF=81=E5=AF=B9=E5=85=B6=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/system/login.lua | 27 +++++++++++--- src/test/test.lua | 66 +++++++++++++++++++++++++--------- src/validator/system/login.lua | 26 ++++++++++---- 3 files changed, 91 insertions(+), 28 deletions(-) diff --git a/src/service/system/login.lua b/src/service/system/login.lua index c8a7450..4f40500 100644 --- a/src/service/system/login.lua +++ b/src/service/system/login.lua @@ -20,16 +20,25 @@ function _M.login() ngx.req.read_body() --获取请求数据 local body_data = ngx.req.get_body_data() + --验证json数据是否正确 + local ok, data = pcall(cjson.decode, body_data) + if not ok then + print("JSON解析失败:", data) + local result = resp:json(0x000001) + resp:send(result) + return + end -- 验证数据是否符合json - local retJson = validator.validatorJson(body_data) + local valid, errors = validator.validateJson(data) --验证失败则返回 - if not retJson then + if not valid then + print("验证失败: ", errors) local result = resp:json(0x000001) resp:send(result) return end --ngx.say(body_data) - local code, ret = loginDao.login(cjson.decode(body_data)) + local code, ret = loginDao.login(data) --读取数据错误 if code ~= 0 or table.getn(ret) < 0 then local result = resp:json(0x000001) @@ -55,8 +64,16 @@ function _M.signup() ngx.req.read_body() --获取请求数据 local body_data = ngx.req.get_body_data() + --验证json数据是否正确 + local ok, data = pcall(cjson.decode, body_data) + if not ok then + print("JSON解析失败:", data) + local result = resp:json(0x000001) + resp:send(result) + return + end -- 验证数据是否符合json - local retJson = validator.validatorJson(body_data) + local retJson = validator.validateJson(data) --验证失败则返回 if not retJson then local result = resp:json(0x000001) @@ -64,7 +81,7 @@ function _M.signup() return end --ngx.say(body_data) - local code, ret = loginDao.signup(cjson.decode(body_data)) + local code, ret = loginDao.signup(data) --读取数据错误 if code ~= 0 or table.getn(ret) < 0 then local result = resp:json(0x000001) diff --git a/src/test/test.lua b/src/test/test.lua index 80a6adb..bc4f2e1 100644 --- a/src/test/test.lua +++ b/src/test/test.lua @@ -63,34 +63,68 @@ ngx.say("code:"..code) ngx.say("-----") -- 正确的schema定义 -local schemaLogin = { +local schema = { type = "object", properties = { - username = {type = "string"}, - password = {type = "string"} + username = { type = "string" }, + password = { type = "string" }, + captcha = { + type = { "string", "null" }, -- 允许string或null + minLength = 0, -- 允许空字符串 + default = "" -- 默认值 + }, + checkKey = { + type = { "string", "null" }, + minLength = 0, + default = "" + } }, - required = {"username", "password"} + required = { "username", "password" } -- 必填字段 } --- JSON数据验证函数 -function validatorLogin(jsonData) - local validator = jsonschema.generate_validator(schemaLogin) - local valid, errors = validator(jsonData) +-- 原始JSON字符串 +--local jsonStr = [[ +--{ +-- "username": "admin", +-- "password": "123456", +-- "captcha": "", +-- "checkKey": "" +--} +--]] +ngx.req.read_body() +--获取请求数据 +local jsonStr = ngx.req.get_body_data() +print("read data:",jsonStr) + +-- JSON解析函数 +local function parse_json(json_str) + local ok, data = pcall(cjson.decode, json_str) + if not ok then + return nil, "JSON解析失败: "..data + end + return data +end + +-- 验证函数 +local function validateJson(data) + local validator = jsonschema.generate_validator(schema) + local valid, errors = validator(data) -- 注意返回两个值 + if not valid then return false, errors end return true end --- 使用示例 -local testData = { - username = "testuser", - password = "mypassword" -} - -local isValid = validatorLogin(testData) -print(isValid and "验证通过" or "验证失败") +-- 完整测试流程 +local testData, err = parse_json(jsonStr) +if testData then + local isValid, validationErr = validateJson(testData) + ngx.say(isValid and "验证通过" or "验证失败: "..(validationErr or "未知错误")) +else + ngx.say("验证失败: ".."未知错误") +end do return end diff --git a/src/validator/system/login.lua b/src/validator/system/login.lua index 8fc3a0c..3f65716 100644 --- a/src/validator/system/login.lua +++ b/src/validator/system/login.lua @@ -13,17 +13,29 @@ local schema = { properties = { username = { type = "string" }, password = { type = "string" }, - captcha = { type = "string" }, - checkKey = { type = "string" }, + captcha = { + type = { "string", "null" }, -- 允许string或null + minLength = 0, -- 允许空字符串 + default = "" -- 默认值 + }, + checkKey = { + type = { "string", "null" }, + minLength = 0, + default = "" + } }, - required = { "username", "password" } + required = { "username", "password" } -- 必填字段 } -function _M.validatorJson(jsonData) - -- 验证数据是否符合schema +-- 验证函数 +function _M.validateJson(data) local validator = jsonschema.generate_validator(schema) - local result = validator(jsonData) - return result + local valid, errors = validator(data) -- 注意返回两个值 + + if not valid then + return false, errors + end + return true end return _M \ No newline at end of file