修改用户登录并进行验证返回用户信息和token数据,修改jsonschema验证方法使用错误问题
This commit is contained in:
parent
899496bd0d
commit
4f4ab0d2bb
|
|
@ -29,9 +29,9 @@ http {
|
|||
log_not_found off;
|
||||
access_log off;
|
||||
}
|
||||
|
||||
#登录认证配置
|
||||
include 'auth/auth.conf';
|
||||
|
||||
#数据列表配置
|
||||
include 'system/account.conf';
|
||||
include 'system/application.conf';
|
||||
include 'system/department.conf';
|
||||
|
|
@ -39,13 +39,17 @@ http {
|
|||
include 'system/role.conf';
|
||||
include 'system/user.conf';
|
||||
|
||||
#测试接口文件
|
||||
#测试接口配置
|
||||
location /testSQL {
|
||||
content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testPostgres.lua';
|
||||
}
|
||||
|
||||
location /cjson {
|
||||
content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/test.lua';
|
||||
}
|
||||
#jwt验证进行测试
|
||||
location /api/test {
|
||||
access_by_lua_file /usr/local/openresty/lualib/resty/jwt-auth.lua;
|
||||
proxy_pass http://192.168.147.1:3000;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,10 @@
|
|||
--- Created by frankly.
|
||||
--- DateTime: 2025/10/29 23:36
|
||||
---
|
||||
local userDao = require("dao.user")
|
||||
--引用使用的库文件
|
||||
local model = require("share.model")
|
||||
--创建一个数据表相关的模型
|
||||
local userModel = model:new('sys_user')
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -18,17 +21,17 @@ local function authenticate(name, passwd)
|
|||
return 0x010002, nil
|
||||
end
|
||||
--根据用户进行验证用户是否存在
|
||||
local code, res = userDao:where("name", "=", name):where("password", "=", passwd):get()
|
||||
local code, res = userModel:where("username", "=", name):where("password", "=", passwd):get()
|
||||
if code == 0 and res ~= nil then
|
||||
return code, res
|
||||
end
|
||||
--根据手机号进行验证用户是否存在
|
||||
code, res = userDao:where("phone", "=", name):where("password", "=", passwd):get()
|
||||
code, res = userModel:where("phone", "=", name):where("password", "=", passwd):get()
|
||||
if code == 0 and res ~= nil then
|
||||
return code, res
|
||||
end
|
||||
--根据邮箱进行验证用户是否存在
|
||||
code, res = userDao:where("email", "=", name):where("password", "=", passwd):get()
|
||||
code, res = userModel:where("email", "=", name):where("password", "=", passwd):get()
|
||||
if code == 0 and res ~= nil then
|
||||
return code, res
|
||||
end
|
||||
|
|
@ -39,7 +42,7 @@ end
|
|||
--用户登录业务逻辑处理
|
||||
function _M.login(jsonData)
|
||||
--解析json中的键和数据值
|
||||
local name = jsonData["name"]
|
||||
local name = jsonData["username"]
|
||||
local passwd = jsonData["password"]
|
||||
local captcha = jsonData["captcha"]
|
||||
local checkKey = jsonData["checkKey"]
|
||||
|
|
|
|||
|
|
@ -7,20 +7,22 @@ local resp = require("util.response")
|
|||
local authDao = require("dao.auth")
|
||||
local jwt = require("resty.jwt")
|
||||
local conf = require("config")
|
||||
local validatorJson = require("validator.system.auth")
|
||||
local validatorJson = require("validator.auth.auth")
|
||||
local cjson = require("cjson.safe")
|
||||
|
||||
local _M = {}
|
||||
|
||||
--设置JWT的有效载荷
|
||||
local obj = {
|
||||
header = {typ="JWT", alg="HS256"},
|
||||
payload = { -- 自定义数据
|
||||
username = "",
|
||||
role = "",
|
||||
--iss = "your_issuer", -- 签发者
|
||||
payload = { -- 自定义数据
|
||||
userid = "", -- 用户id
|
||||
username = "", -- 用户名
|
||||
role = "", -- 角色
|
||||
--iss = "your_issuer", -- 签发者
|
||||
--sub = "1234567890", -- 主题
|
||||
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时)
|
||||
iat = os.time() -- 签发时间
|
||||
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时)
|
||||
iat = os.time() -- 签发时间
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -31,15 +33,15 @@ function _M.login()
|
|||
--获取请求数据
|
||||
local body_data = ngx.req.get_body_data()
|
||||
-- 验证数据是否符合json
|
||||
local ok = validatorJson.validatorJson(body_data)
|
||||
local retJson = validatorJson.validatorJson(body_data)
|
||||
--验证失败则返回
|
||||
if not ok then
|
||||
if not retJson then
|
||||
local result = resp:json(0x000001)
|
||||
resp:send(result)
|
||||
return
|
||||
end
|
||||
--ngx.say(body_data)
|
||||
local code, ret = authDao.login(body_data)
|
||||
local code, ret = authDao.login(cjson.decode(body_data))
|
||||
--读取数据错误
|
||||
if code ~= 0 or table.getn(ret) < 0 then
|
||||
local result = resp:json(0x000001)
|
||||
|
|
@ -47,11 +49,15 @@ function _M.login()
|
|||
return
|
||||
end
|
||||
--获取的登陆的用户信息,返回tocken
|
||||
obj.payload.username = body_data["name"]
|
||||
obj.payload.userid = ret["id"]
|
||||
obj.payload.username = ret["name"]
|
||||
obj.payload.role = ""
|
||||
local jwt_token = jwt:sign(conf.secret_key, obj)
|
||||
ngx.say(jwt_token)
|
||||
local result = resp:json(code, ret)
|
||||
--ngx.say(jwt_token)
|
||||
local data = {}
|
||||
data["token"] = jwt_token
|
||||
data["userInfo"] = ret
|
||||
local result = resp:json(code, data)
|
||||
resp:send(result)
|
||||
end
|
||||
|
||||
|
|
@ -70,7 +76,7 @@ function _M.logout()
|
|||
return
|
||||
end
|
||||
--ngx.say(body_data)
|
||||
local code, ret = authDao.logout(body_data)
|
||||
local code, ret = authDao.logout(cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
local resp = require("util.response")
|
||||
local accountDao = require("dao.account")
|
||||
local validatorJson = require("validator.system.account")
|
||||
local cjson = require("cjson.safe")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -41,7 +42,7 @@ function _M.addSystemAccount()
|
|||
return
|
||||
end
|
||||
--ngx.say(body_data)
|
||||
local code, ret = accountDao.addSystemAccount(body_data)
|
||||
local code, ret = accountDao.addSystemAccount(cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
@ -67,7 +68,7 @@ function _M.updateSystemAccount(m)
|
|||
resp:send(result)
|
||||
return
|
||||
end
|
||||
local code, ret = accountDao.updateSystemAccount(m.id, body_data)
|
||||
local code, ret = accountDao.updateSystemAccount(m.id, cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
local resp = require("util.response")
|
||||
local applicationDao = require("dao.application")
|
||||
local validatorJson = require("validator.system.application")
|
||||
local cjson = require("cjson.safe")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -55,7 +56,7 @@ function _M.addSystemApplication()
|
|||
return
|
||||
end
|
||||
--ngx.say(body_data)
|
||||
local code, ret = applicationDao.addApplication(body_data)
|
||||
local code, ret = applicationDao.addApplication(cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
@ -81,7 +82,7 @@ function _M.updateSystemApplication(m)
|
|||
resp:send(result)
|
||||
return
|
||||
end
|
||||
local code, ret = applicationDao.updateSystemApplication(m.id, body_data)
|
||||
local code, ret = applicationDao.updateSystemApplication(m.id, cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
local resp = require("util.response")
|
||||
local departmentDao = require("dao.department")
|
||||
local validatorJson = require("validator.system.department")
|
||||
local cjson = require("cjson.safe")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -41,7 +42,7 @@ function _M.addSystemDepartment()
|
|||
return
|
||||
end
|
||||
--ngx.say(body_data)
|
||||
local code, ret = departmentDao.addSystemDepartment(body_data)
|
||||
local code, ret = departmentDao.addSystemDepartment(cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
@ -67,7 +68,7 @@ function _M.updateSystemDepartment(m)
|
|||
resp:send(result)
|
||||
return
|
||||
end
|
||||
local code, ret = departmentDao.updateSystemDepartment(m.id, body_data)
|
||||
local code, ret = departmentDao.updateSystemDepartment(m.id, cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
local resp = require("util.response")
|
||||
local permissionDao = require("dao.permission")
|
||||
local validatorJson = require("validator.system.permission")
|
||||
local cjson = require("cjson.safe")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ function _M.addSystemPermission()
|
|||
return
|
||||
end
|
||||
--ngx.say(body_data)
|
||||
local code, ret = permissionDao.addPermission(body_data)
|
||||
local code, ret = permissionDao.addPermission(cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
@ -74,7 +75,7 @@ function _M.updateSystemPermission(m)
|
|||
resp:send(result)
|
||||
return
|
||||
end
|
||||
local code, ret = permissionDao.updatePermission(m.id, body_data)
|
||||
local code, ret = permissionDao.updatePermission(m.id, cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
local resp = require("util.response")
|
||||
local roleDao = require("dao.role")
|
||||
local validatorJson = require("validator.system.role")
|
||||
local cjson = require("cjson.safe")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -42,7 +43,7 @@ function _M.addSystemRole()
|
|||
return
|
||||
end
|
||||
--ngx.say(body_data)
|
||||
local code, ret = roleDao.addSystemRole(body_data)
|
||||
local code, ret = roleDao.addSystemRole(cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
@ -68,7 +69,7 @@ function _M.updateSystemRole(m)
|
|||
resp:send(result)
|
||||
return
|
||||
end
|
||||
local code, ret = roleDao.updateSystemRole(m.id, body_data)
|
||||
local code, ret = roleDao.updateSystemRole(m.id, cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
local resp = require("util.response")
|
||||
local userDao = require("dao.user")
|
||||
local validatorJson = require("validator.system.user")
|
||||
local cjson = require("cjson.safe")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -42,7 +43,7 @@ function _M.addSystemUser()
|
|||
return
|
||||
end
|
||||
--ngx.say(body_data)
|
||||
local code, ret = userDao.addSystemUser(body_data)
|
||||
local code, ret = userDao.addSystemUser(cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
@ -69,7 +70,7 @@ function _M.updateSystemUser(m)
|
|||
return
|
||||
end
|
||||
--将数据更新到数据表中
|
||||
local code, ret = userDao.updateSystemUser(m.id, body_data)
|
||||
local code, ret = userDao.updateSystemUser(m.id, cjson.decode(body_data))
|
||||
local result = resp:json(code, ret)
|
||||
resp:send(result)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -32,6 +32,17 @@ local pageSize = args["pagesize"] or 10
|
|||
ngx.say("pageNum:", pageNum, " pageSize:", pageSize)
|
||||
--]]
|
||||
|
||||
|
||||
local jwttoken = require("validator.auth.auth")
|
||||
--获取请求头中的令牌数据
|
||||
local auth_header = ngx.var.http_Authorization
|
||||
--调用令牌校验
|
||||
local result = jwttoken.check(auth_header)
|
||||
-- 输出结果
|
||||
ngx.say(cjson.encode(result))
|
||||
ngx.exit(result.code)
|
||||
|
||||
--[[
|
||||
local jsonschema = require("jsonschema")
|
||||
|
||||
-- 定义一个JSON Schema
|
||||
|
|
@ -67,6 +78,7 @@ if not ok then
|
|||
else
|
||||
print("Validation succeeded!")
|
||||
end
|
||||
--]]
|
||||
|
||||
--[[
|
||||
local jwt = require("resty.jwt")
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
--- DateTime: 2025/10/30 08:09
|
||||
---业务逻辑 对账户登录的参数进行数据的验证
|
||||
local jsonschema = require("jsonschema")
|
||||
local jwt = require("resty.jwt")
|
||||
local conf = require("config")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -19,8 +21,42 @@ local schema = {
|
|||
|
||||
function _M.validatorJson(jsonData)
|
||||
-- 验证数据是否符合schema
|
||||
local ok, err = jsonschema:generate_validator(jsonData, schema)
|
||||
return ok
|
||||
local validator = jsonschema.generate_validator(schema)
|
||||
local result = validator(jsonData)
|
||||
return result
|
||||
end
|
||||
|
||||
--令牌校验
|
||||
function _M.check(auth_header)
|
||||
--定义响应数据
|
||||
local response = {}
|
||||
--如果请求头中没有令牌,则直接返回401
|
||||
if auth_header == nil then
|
||||
response["code"] = 401
|
||||
response["message"] = "没有找到令牌数据"
|
||||
return response
|
||||
end
|
||||
--查找令牌中的Bearer前缀字符,并进行截取
|
||||
local _, _, token = string.find(auth_header, "Bearer%s+(.+)")
|
||||
--如果没有Bearer,则表示令牌无效
|
||||
if token == nil then
|
||||
response["code"] = 401
|
||||
response["message"] = "令牌格式不正确"
|
||||
return response
|
||||
end
|
||||
--校验令牌
|
||||
local jwt_obj = jwt:verify(conf.secret_key, token)
|
||||
--如果校验结果中的verified==false,则表示令牌无效
|
||||
if jwt_obj.verified == false then
|
||||
response["code"] = 401
|
||||
response["message"] = "令牌无效"
|
||||
return response
|
||||
end
|
||||
--全部校验完成后,说明令牌有效,返回令牌数据
|
||||
response["code"] = 200
|
||||
response["message"] = "令牌校验通过"
|
||||
response["body"] = jwt_obj
|
||||
return response
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
@ -22,8 +22,9 @@ local schema = {
|
|||
|
||||
function _M.validatorJson(jsonData)
|
||||
-- 验证数据是否符合schema
|
||||
local ok, err = jsonschema:generate_validator(jsonData, schema)
|
||||
return ok
|
||||
local validator = jsonschema.generate_validator(schema)
|
||||
local result = validator(jsonData)
|
||||
return result
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -24,8 +24,9 @@ local schema = {
|
|||
|
||||
function _M.validatorJson(jsonData)
|
||||
-- 验证数据是否符合schema
|
||||
local ok, err = jsonschema:generate_validator(jsonData, schema)
|
||||
return ok
|
||||
local validator = jsonschema.generate_validator(schema)
|
||||
local result = validator(jsonData)
|
||||
return result
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -23,8 +23,9 @@ local schema = {
|
|||
|
||||
function _M.validatorJson(jsonData)
|
||||
-- 验证数据是否符合schema
|
||||
local ok, err = jsonschema:generate_validator(jsonData, schema)
|
||||
return ok
|
||||
local validator = jsonschema.generate_validator(schema)
|
||||
local result = validator(jsonData)
|
||||
return result
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -23,8 +23,9 @@ local schema = {
|
|||
|
||||
function _M.validatorJson(jsonData)
|
||||
-- 验证数据是否符合schema
|
||||
local ok, err = jsonschema:generate_validator(jsonData, schema)
|
||||
return ok
|
||||
local validator = jsonschema.generate_validator(schema)
|
||||
local result = validator(jsonData)
|
||||
return result
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -19,8 +19,9 @@ local schema = {
|
|||
|
||||
function _M.validatorJson(jsonData)
|
||||
-- 验证数据是否符合schema
|
||||
local ok, err = jsonschema:generate_validator(jsonData, schema)
|
||||
return ok
|
||||
local validator = jsonschema.generate_validator(schema)
|
||||
local result = validator(jsonData)
|
||||
return result
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -23,8 +23,9 @@ local schema = {
|
|||
|
||||
function _M.validatorJson(jsonData)
|
||||
-- 验证数据是否符合schema
|
||||
local ok, err = jsonschema:generate_validator(jsonData, schema)
|
||||
return ok
|
||||
local validator = jsonschema.generate_validator(schema)
|
||||
local result = validator(jsonData)
|
||||
return result
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user