修改系统名称,修改配置文件错误,增加登录验证相关逻辑

This commit is contained in:
wanglei 2025-10-29 23:54:17 +08:00
parent 96245e79a7
commit 4022a395c8
8 changed files with 140 additions and 122 deletions

View File

@ -1,4 +1,4 @@
#API接口文件
location /api/system/auth {
content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/system/auth.lua';
location /api/auth {
content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/auth/auth.lua';
}

View File

@ -6,7 +6,7 @@
--解析url路由过滤库
local radix = require("resty.radixtree")
--数据表业务处理
local systemAuth = require("service.system.auth")
local authService = require("service.auth.auth")
--定义相关路由前端接口url地址
local routes = {
@ -15,15 +15,15 @@ local routes = {
--------------------------------------------
--用户登录路由接口
{
paths = { "/api/login" },
paths = { "/api/auth/login" },
methods = { "POST" },
handler = systemAuth.login,
handler = authService.login,
},
--用户退出路由接口
{
paths = { "/api/logout/:id" },
paths = { "/api/auth/logout/:id" },
methods = { "POST" },
handler = systemAuth.logout,
handler = authService.logout,
},
}

View File

@ -11,6 +11,8 @@ return {
time_zone = "+8:00", -- UTC + 8
secret_key = "!@#$5412$#@!", -- 确保这个密钥足够安全并保密
REDIS_PREFIX = 'Auth:',
-- 配置redis数据库连接
REDIS = {

69
src/dao/auth.lua Normal file
View File

@ -0,0 +1,69 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by frankly.
--- DateTime: 2025/10/29 23:36
---
local userDao = require("dao.user")
local _M = {}
--认证用户返回用户数据信息
local function authenticate(name, passwd)
--验证用户名是否为空
if name == "" then
return 0x010003, nil
end
--验证密码是否为空
if passwd == "" then
return 0x010002, nil
end
--根据用户进行验证用户是否存在
local code, res = userDao:where("name", "=", 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()
if code == 0 and res ~= nil then
return code, res
end
--根据邮箱进行验证用户是否存在
code, res = userDao:where("email", "=", name):where("password", "=", passwd):get()
if code == 0 and res ~= nil then
return code, res
end
--查询不到用户信息
return 0x010003, nil
end
--用户登录业务逻辑处理
function _M.login(jsonData)
--解析json中的键和数据值
local name = jsonData["name"]
local passwd = jsonData["password"]
local captcha = jsonData["captcha"]
local checkKey = jsonData["checkKey"]
--验证用户名是否为空
local code, res = authenticate(name, passwd)
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
--用户存在时返回用户已经存在
if num <= 0 then
return 0x01000C,nil
end
return 0, res
end
--用户登出业务逻辑处理
function _M.logout(jsonData)
local code = 0
local ret = "{}"
return code, ret
end
return _M

View File

@ -3,46 +3,70 @@
--- Created by admin.
--- DateTime: 2025/10/28 11:09
---
local helpers = require("share.helpers")
local jsonschema = require("jsonschema")
local resp = require("util.response")
local user = require("model.user")
local authDao = require("dao.auth")
local jwt = require("resty.jwt")
local conf = require("config")
local _M = {}
local dao = require("service.system.auth")
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "username", type = "string"},
{name = "password", type = "string"},
{name = "captcha", type = "string"},
{name = "checkKey", type = "string"},
}, required = {"username", "password"}}
}
--设置JWT的有效载荷
local obj = {
header = {typ="JWT", alg="HS256"},
payload = { -- 自定义数据
username = "",
role = "",
--iss = "your_issuer", -- 签发者
--sub = "1234567890", -- 主题
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时
iat = os.time() -- 签发时间
}
}
--用户登录业务逻辑处理
function _M.login()
--获取请求头并进行校验
if validator.checkReqHeader() == false then
local result = resp:json(0x000001)
resp:send(result)
return
end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
--判断请求体数据是否为空
if body_data == nil then
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = dao.login(body_data)
local code, ret = authDao.login(body_data)
--读取数据错误
if code ~= 0 or table.getn(ret) < 0 then
local result = resp:json(0x000001)
resp:send(result)
return
end
--获取的登陆的用户信息返回tocken
obj.payload.username = body_data["name"]
obj.payload.role = ""
local jwt_token = jwt:sign(conf.secret_key, obj)
ngx.say(jwt_token)
local result = resp:json(code, ret)
resp:send(result)
end
--用户登出业务逻辑处理
function _M.logout()
--获取请求头并进行校验
if validator.checkReqHeader() == false then
local result = resp:json(0x000001)
resp:send(result)
return
end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
@ -54,82 +78,7 @@ function _M.logout()
return
end
--ngx.say(body_data)
local code, ret = dao.logout(body_data)
local result = resp:json(code, ret)
resp:send(result)
end
--认证用户返回用户数据信息
local function authenticate(name, passwd)
--验证用户名是否为空
if name == "" then
return 0x010003, nil
end
--验证密码是否为空
if passwd == "" then
return 0x010002, nil
end
--根据用户进行验证用户是否存在
local code, res = user:where("name", "=", name):where("password", "=", passwd):get()
if code == 0 and res ~= nil then
return code, res
end
--根据手机号进行验证用户是否存在
code, res = user:where("phone", "=", name):where("password", "=", passwd):get()
if code == 0 and res ~= nil then
return code, res
end
--根据邮箱进行验证用户是否存在
code, res = user:where("email", "=", name):where("password", "=", passwd):get()
if code == 0 and res ~= nil then
return code, res
end
--查询不到用户信息
return 0x010003, nil
end
--用户登录业务逻辑处理
function _M.login(jsonData)
--验证数据的正确性,错误时返回
local success, result = validator.checkJson(jsonData)
if success == false then
return 0x000001,result
end
--解析json中的键和数据值
local name = ""
local passwd = ""
local captcha = ""
local checkKey = ""
for key, value in pairs(result) do
if key == "username" then name = value end
if key == "password" then passwd = value end
if key == "captcha" then captcha = value end
if key == "checkKey" then checkKey = value end
end
--验证用户名是否为空
local code, res = authenticate(name, passwd)
if code ~= 0 then
return 0x000001,res
end
local num = 0
for _, row in ipairs(res) do
for key, value in pairs(row) do
num = num + 1
end
end
--用户存在时返回用户已经存在
if num <= 0 then
return 0x01000C,nil
end
--对用户进行认证返回相关的数据
local result = resp:json(code, res)
resp:send(result)
end
--用户登出业务逻辑处理
function _M.logout(jsonData)
local code = 0
local ret = "{}"
local code, ret = authDao.logout(body_data)
local result = resp:json(code, ret)
resp:send(result)
end

View File

@ -14,8 +14,6 @@ local mt = { __index = _M }
function _M:get_connection()
local code = 0
-- modify by wanglei : 修改添加支持postgresql连接
if ngx.ctx[self.db_type] then
-- if write before read, make sure write read connection the same
if ngx.ctx[WRITE] then
@ -35,7 +33,7 @@ function _M:get_connection()
---- 连接到数据库
local ok, err = conn:connect()
if not ok then
print("Connection failed: " .. err)
ngx.log(ngx.ERR, "Connection failed: " .. err)
code = 0x000002
end
ngx.log(ngx.INFO, 'Connection success')

View File

@ -12,28 +12,28 @@ local WRITE = 'WRITE'
local READ = 'READ'
local database_write = Database:new({
host = conf.POSTGRES.host,
port = conf.POSTGRES.port,
user = conf.POSTGRES.user,
password = conf.POSTGRES.password,
database = conf.POSTGRES.database,
--charset = conf.POSTGRES.charset,
--timeout = conf.POSTGRES.timeout,
db_pool_timeout = conf.POSTGRES.pool_timeout,
db_pool_size = conf.POSTGRES.pool_size,
host = conf.POSTGRES.HOST,
port = conf.POSTGRES.PORT,
user = conf.POSTGRES.USERNAME,
password = conf.POSTGRES.PASSWORD,
database = conf.POSTGRES.DATABASE,
charset = conf.POSTGRES.CHARSET,
timeout = conf.POSTGRES.TIMEOUT,
db_pool_timeout = conf.POSTGRES.POOL_TIMEOUT,
db_pool_size = conf.POSTGRES.POOL_SIZE,
db_type = WRITE
})
local database_read = Database:new({
host = conf.POSTGRES.host,
port = conf.POSTGRES.port,
user = conf.POSTGRES.user,
password = conf.POSTGRES.password,
database = conf.POSTGRES.database,
--charset = conf.POSTGRES.charset,
--timeout = conf.POSTGRES.timeout,
db_pool_timeout = conf.POSTGRES.pool_timeout,
db_pool_size = conf.POSTGRES.pool_size,
host = conf.POSTGRES.HOST,
port = conf.POSTGRES.PORT,
user = conf.POSTGRES.USERNAME,
password = conf.POSTGRES.PASSWORD,
database = conf.POSTGRES.DATABASE,
charset = conf.POSTGRES.CHARSET,
timeout = conf.POSTGRES.TIMEOUT,
db_pool_timeout = conf.POSTGRES.POOL_TIMEOUT,
db_pool_size = conf.POSTGRES.POOL_SIZE,
db_type = READ
})