修改系统名称,修改配置文件错误,增加登录验证相关逻辑
This commit is contained in:
parent
96245e79a7
commit
4022a395c8
|
|
@ -1,4 +1,4 @@
|
||||||
#API接口文件
|
#API接口文件
|
||||||
location /api/system/auth {
|
location /api/auth {
|
||||||
content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/system/auth.lua';
|
content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/auth/auth.lua';
|
||||||
}
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
--解析url路由过滤库
|
--解析url路由过滤库
|
||||||
local radix = require("resty.radixtree")
|
local radix = require("resty.radixtree")
|
||||||
--数据表业务处理
|
--数据表业务处理
|
||||||
local systemAuth = require("service.system.auth")
|
local authService = require("service.auth.auth")
|
||||||
|
|
||||||
--定义相关路由,前端接口url地址
|
--定义相关路由,前端接口url地址
|
||||||
local routes = {
|
local routes = {
|
||||||
|
|
@ -15,15 +15,15 @@ local routes = {
|
||||||
--------------------------------------------
|
--------------------------------------------
|
||||||
--用户登录路由接口
|
--用户登录路由接口
|
||||||
{
|
{
|
||||||
paths = { "/api/login" },
|
paths = { "/api/auth/login" },
|
||||||
methods = { "POST" },
|
methods = { "POST" },
|
||||||
handler = systemAuth.login,
|
handler = authService.login,
|
||||||
},
|
},
|
||||||
--用户退出路由接口
|
--用户退出路由接口
|
||||||
{
|
{
|
||||||
paths = { "/api/logout/:id" },
|
paths = { "/api/auth/logout/:id" },
|
||||||
methods = { "POST" },
|
methods = { "POST" },
|
||||||
handler = systemAuth.logout,
|
handler = authService.logout,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ return {
|
||||||
|
|
||||||
time_zone = "+8:00", -- UTC + 8
|
time_zone = "+8:00", -- UTC + 8
|
||||||
|
|
||||||
|
secret_key = "!@#$5412$#@!", -- 确保这个密钥足够安全并保密
|
||||||
|
|
||||||
REDIS_PREFIX = 'Auth:',
|
REDIS_PREFIX = 'Auth:',
|
||||||
-- 配置redis数据库连接
|
-- 配置redis数据库连接
|
||||||
REDIS = {
|
REDIS = {
|
||||||
|
|
|
||||||
69
src/dao/auth.lua
Normal file
69
src/dao/auth.lua
Normal 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
|
||||||
|
|
@ -3,46 +3,70 @@
|
||||||
--- Created by admin.
|
--- Created by admin.
|
||||||
--- DateTime: 2025/10/28 11:09
|
--- DateTime: 2025/10/28 11:09
|
||||||
---
|
---
|
||||||
local helpers = require("share.helpers")
|
local jsonschema = require("jsonschema")
|
||||||
local resp = require("util.response")
|
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 _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()
|
function _M.login()
|
||||||
--获取请求头并进行校验
|
|
||||||
if validator.checkReqHeader() == false then
|
|
||||||
local result = resp:json(0x000001)
|
|
||||||
resp:send(result)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
--读取请求体的数据
|
--读取请求体的数据
|
||||||
ngx.req.read_body()
|
ngx.req.read_body()
|
||||||
--获取请求数据
|
--获取请求数据
|
||||||
local body_data = ngx.req.get_body_data()
|
local body_data = ngx.req.get_body_data()
|
||||||
--判断请求体数据是否为空
|
-- 验证数据是否符合schema
|
||||||
if body_data == nil then
|
local ok, err = jsonschema:generate_validator(body_data, schema)
|
||||||
|
--验证失败则返回
|
||||||
|
if not ok then
|
||||||
local result = resp:json(0x000001)
|
local result = resp:json(0x000001)
|
||||||
resp:send(result)
|
resp:send(result)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
--ngx.say(body_data)
|
--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)
|
local result = resp:json(code, ret)
|
||||||
resp:send(result)
|
resp:send(result)
|
||||||
end
|
end
|
||||||
|
|
||||||
--用户登出业务逻辑处理
|
--用户登出业务逻辑处理
|
||||||
function _M.logout()
|
function _M.logout()
|
||||||
--获取请求头并进行校验
|
|
||||||
if validator.checkReqHeader() == false then
|
|
||||||
local result = resp:json(0x000001)
|
|
||||||
resp:send(result)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
--读取请求体的数据
|
--读取请求体的数据
|
||||||
ngx.req.read_body()
|
ngx.req.read_body()
|
||||||
--获取请求数据
|
--获取请求数据
|
||||||
|
|
@ -54,82 +78,7 @@ function _M.logout()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
--ngx.say(body_data)
|
--ngx.say(body_data)
|
||||||
local code, ret = dao.logout(body_data)
|
local code, ret = authDao.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 result = resp:json(code, ret)
|
local result = resp:json(code, ret)
|
||||||
resp:send(result)
|
resp:send(result)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,6 @@ local mt = { __index = _M }
|
||||||
function _M:get_connection()
|
function _M:get_connection()
|
||||||
local code = 0
|
local code = 0
|
||||||
-- modify by wanglei : 修改添加支持postgresql连接
|
-- modify by wanglei : 修改添加支持postgresql连接
|
||||||
|
|
||||||
|
|
||||||
if ngx.ctx[self.db_type] then
|
if ngx.ctx[self.db_type] then
|
||||||
-- if write before read, make sure write read connection the same
|
-- if write before read, make sure write read connection the same
|
||||||
if ngx.ctx[WRITE] then
|
if ngx.ctx[WRITE] then
|
||||||
|
|
@ -35,7 +33,7 @@ function _M:get_connection()
|
||||||
---- 连接到数据库
|
---- 连接到数据库
|
||||||
local ok, err = conn:connect()
|
local ok, err = conn:connect()
|
||||||
if not ok then
|
if not ok then
|
||||||
print("Connection failed: " .. err)
|
ngx.log(ngx.ERR, "Connection failed: " .. err)
|
||||||
code = 0x000002
|
code = 0x000002
|
||||||
end
|
end
|
||||||
ngx.log(ngx.INFO, 'Connection success')
|
ngx.log(ngx.INFO, 'Connection success')
|
||||||
|
|
|
||||||
|
|
@ -12,28 +12,28 @@ local WRITE = 'WRITE'
|
||||||
local READ = 'READ'
|
local READ = 'READ'
|
||||||
|
|
||||||
local database_write = Database:new({
|
local database_write = Database:new({
|
||||||
host = conf.POSTGRES.host,
|
host = conf.POSTGRES.HOST,
|
||||||
port = conf.POSTGRES.port,
|
port = conf.POSTGRES.PORT,
|
||||||
user = conf.POSTGRES.user,
|
user = conf.POSTGRES.USERNAME,
|
||||||
password = conf.POSTGRES.password,
|
password = conf.POSTGRES.PASSWORD,
|
||||||
database = conf.POSTGRES.database,
|
database = conf.POSTGRES.DATABASE,
|
||||||
--charset = conf.POSTGRES.charset,
|
charset = conf.POSTGRES.CHARSET,
|
||||||
--timeout = conf.POSTGRES.timeout,
|
timeout = conf.POSTGRES.TIMEOUT,
|
||||||
db_pool_timeout = conf.POSTGRES.pool_timeout,
|
db_pool_timeout = conf.POSTGRES.POOL_TIMEOUT,
|
||||||
db_pool_size = conf.POSTGRES.pool_size,
|
db_pool_size = conf.POSTGRES.POOL_SIZE,
|
||||||
db_type = WRITE
|
db_type = WRITE
|
||||||
})
|
})
|
||||||
|
|
||||||
local database_read = Database:new({
|
local database_read = Database:new({
|
||||||
host = conf.POSTGRES.host,
|
host = conf.POSTGRES.HOST,
|
||||||
port = conf.POSTGRES.port,
|
port = conf.POSTGRES.PORT,
|
||||||
user = conf.POSTGRES.user,
|
user = conf.POSTGRES.USERNAME,
|
||||||
password = conf.POSTGRES.password,
|
password = conf.POSTGRES.PASSWORD,
|
||||||
database = conf.POSTGRES.database,
|
database = conf.POSTGRES.DATABASE,
|
||||||
--charset = conf.POSTGRES.charset,
|
charset = conf.POSTGRES.CHARSET,
|
||||||
--timeout = conf.POSTGRES.timeout,
|
timeout = conf.POSTGRES.TIMEOUT,
|
||||||
db_pool_timeout = conf.POSTGRES.pool_timeout,
|
db_pool_timeout = conf.POSTGRES.POOL_TIMEOUT,
|
||||||
db_pool_size = conf.POSTGRES.pool_size,
|
db_pool_size = conf.POSTGRES.POOL_SIZE,
|
||||||
db_type = READ
|
db_type = READ
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user