用户登录进行json验证对其进行修改

This commit is contained in:
wanglei 2025-11-12 14:41:58 +08:00
parent 8c9015fe06
commit e8fe6be8de
3 changed files with 91 additions and 28 deletions

View File

@ -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)

View File

@ -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

View File

@ -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