Compare commits

..

5 Commits

31 changed files with 706 additions and 44 deletions

View File

@ -13,8 +13,10 @@ http {
client_max_body_size 1024M; #允许最大100k的请求体
client_body_buffer_size 1024M; #设置缓冲区大小
#lua_code_cache off; #关闭代码缓存修改lua脚本不需要重启
lua_package_path '$prefix/src/?/?.lua;$prefix/src/?.lua;/home/frankly/work/AuthPlatform/src/?/?.lua;/home/frankly/work/AuthPlatform/src/?.lua;;';
lua_package_cpath '$prefix/src/share/lib/?.so;;';
lua_package_cpath '$prefix/src/share/lib/?.so;/home/frankly/work/AuthPlatform/src/share/lib/?.so;;';
# Path of the file with trusted CA certificates.
#lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
@ -22,6 +24,21 @@ http {
# The verification depth in the server certificates chain.
#lua_ssl_verify_depth 3;
#在Nginx启动时执行的Lua代码块
lua_shared_dict dict_a 1m;
init_by_lua_block {
-- 定义一个全局变量
ngx.log(ngx.INFO, "Initializing global variable")
global_var = "Hello, Nginx with Lua!"
-- 初始化一个共享字典(需要 lua-shared-dict 模块)
local shared_dict = ngx.shared.dict_a
shared_dict:set("key", "value")
}
#init_by_lua_block init_by_lua_file 只能初始化其中的一个,不能同时启用
#否则报错nginx: [emerg] "init_by_lua_file" directive is duplicate
#init_by_lua_file '/home/frankly/work/AuthPlatform/src/init.lua';
server {
listen 9080;
server_name 127.0.0.1;
@ -33,7 +50,6 @@ http {
## 应用路径 todo 路径问题
set $APP_PATH '/home/frankly/work/AuthPlatform';
#access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
#数据列表配置
include 'system/system.conf';
@ -41,13 +57,25 @@ http {
location /testSQL {
content_by_lua_file '${APP_PATH}/src/test/testPostgres.lua';
}
location /testRBAC {
content_by_lua_file '${APP_PATH}/src/test/testRBAC.lua';
}
location /cjson {
content_by_lua_file '${APP_PATH}/src/test/test.lua';
}
#jwt验证进行测试
location /api/test {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
proxy_pass http://192.168.147.1:3000;
location = /testSM {
content_by_lua_block {
cjson = require "cjson.safe"
ngx.say(cjson.encode({a = 1, b = 2}))
local dict_a = ngx.shared.dict_a;
ngx.say("abc=",dict_a:get("abc"))
-- 访问全局变量
ngx.say("Global variable: ", global_var)
-- 访问共享字典
ngx.say("Shared dict value: ", dict_a:get("key"))
}
}
}
}

55
conf/system/system.conf Normal file
View File

@ -0,0 +1,55 @@
######################################################
### 接口相关控制接口文件需要使用jwt进行token验证 ###
######################################################
#用户认证登陆相关
location /api/auth {
content_by_lua_file '${APP_PATH}/src/api/auth/auth.lua';
}
#账号信息数据接口
location /api/system/accounts {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/account.lua';
}
#应用程序信息数据接口
location /api/system/applications {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/application.lua';
}
#组织(岗位)信息数据接口
location /api/system/departments {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/department.lua';
}
#菜单信息数据接口
location /api/system/menus {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/menu.lua';
}
#权限信息数据接口
location /api/system/permissions {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/permission.lua';
}
#岗位信息数据接口
location /api/system/positions {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/position.lua';
}
#账号信息数据接口
location /api/system/roles {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/role.lua';
}
#用户信息数据接口
location /api/system/users {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/user.lua';
}

View File

@ -11,7 +11,7 @@ local systemAccount = require("service.system.account")
--定义相关路由前端接口url地址
local routes = {
--户相关路由接口
--户相关路由接口
{
paths = { "/api/system/accounts" },
methods = { "GET" },

View File

@ -11,7 +11,7 @@ local systemApplication = require("service.system.application")
--定义相关路由前端接口url地址
local routes = {
--相关路由接口
--用相关路由接口
{
paths = { "/api/system/applications" },
methods = { "GET" },

View File

@ -7,35 +7,35 @@
--解析url路由过滤库
local radix = require("resty.radixtree")
--数据表业务处理
local systemOrganization = require("service.system.department")
local systemDepartment = require("service.system.department")
--定义相关路由前端接口url地址
local routes = {
--用户相关路由接口
--组织(部门)相关路由接口
{
paths = { "/api/system/organizations" },
paths = { "/api/system/departments" },
methods = { "GET" },
handler = systemOrganization.getSystemOrganization,
handler = systemDepartment.getSystemDepartments,
},
{
paths = { "/api/system/organizations/:id" },
paths = { "/api/system/departments/:id" },
methods = { "GET" },
handler = systemOrganization.getSystemOrganization,
handler = systemDepartment.getSystemDepartment,
},
{
paths = { "/api/system/organizations" },
paths = { "/api/system/departments" },
methods = { "POST" },
handler = systemOrganization.addSystemOrganization,
handler = systemDepartment.addSystemDepartment,
},
{
paths = { "/api/system/organizations/:id" },
paths = { "/api/system/departments/:id" },
methods = { "DELETE" },
handler = systemOrganization.deleteSystemOrganization,
handler = systemDepartment.deleteSystemDepartment,
},
{
paths = { "/api/system/organizations/:id" },
paths = { "/api/system/departments/:id" },
methods = { "PUT" },
handler = systemOrganization.updateSystemOrganization,
handler = systemDepartment.updateSystemDepartment,
},
}

61
src/api/system/menu.lua Normal file
View File

@ -0,0 +1,61 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/11/04 10:45
---
--解析url路由过滤库
local radix = require("resty.radixtree")
--数据表业务处理
local systemMenu = require("service.system.menu")
--定义相关路由前端接口url地址
local routes = {
--菜单相关路由接口
{
paths = { "/api/system/menus" },
methods = { "GET" },
handler = systemMenu.getSystemMenus,
},
{
paths = { "/api/system/menus/:id" },
methods = { "GET" },
handler = systemMenu.getSystemMenu,
},
{
paths = { "/api/system/menus" },
methods = { "POST" },
handler = systemMenu.addSystemMenu,
},
{
paths = { "/api/system/menus/:id" },
methods = { "DELETE" },
handler = systemMenu.deleteSystemMenu,
},
{
paths = { "/api/system/menus/:id" },
methods = { "PUT" },
handler = systemMenu.updateSystemMenu,
},
}
-- 初始化路由
local rx, err = radix.new(routes)
if not rx then
ngx.say("Not Found")
ngx.exit(ngx.HTTP_NOT_FOUND)
end
--获取访问的uri地址
local uri = ngx.var.uri
local opts = {
method = ngx.var.request_method,
matched = {}
}
-- 进行路由匹配和相关函数调用
local ok = rx:dispatch(uri, opts, opts.matched)
if not ok then
ngx.say("Not Found")
ngx.exit(ngx.HTTP_NOT_FOUND)
end

View File

@ -11,7 +11,7 @@ local systemPermission = require("service.system.permission")
--定义相关路由前端接口url地址
local routes = {
--用户相关路由接口
--权限相关路由接口
{
paths = { "/api/system/permissions" },
methods = { "GET" },

View File

@ -0,0 +1,61 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/11/04 10:50
---
--解析url路由过滤库
local radix = require("resty.radixtree")
--数据表业务处理
local systemPosition = require("service.system.position")
--定义相关路由前端接口url地址
local routes = {
--岗位相关路由接口
{
paths = { "/api/system/positions" },
methods = { "GET" },
handler = systemPosition.getSystemPositions,
},
{
paths = { "/api/system/positions/:id" },
methods = { "GET" },
handler = systemPosition.getSystemPosition,
},
{
paths = { "/api/system/positions" },
methods = { "POST" },
handler = systemPosition.addSystemPosition,
},
{
paths = { "/api/system/positions/:id" },
methods = { "DELETE" },
handler = systemPosition.deleteSystemPosition,
},
{
paths = { "/api/system/positions/:id" },
methods = { "PUT" },
handler = systemPosition.updateSystemPosition,
},
}
-- 初始化路由
local rx, err = radix.new(routes)
if not rx then
ngx.say("Not Found")
ngx.exit(ngx.HTTP_NOT_FOUND)
end
--获取访问的uri地址
local uri = ngx.var.uri
local opts = {
method = ngx.var.request_method,
matched = {}
}
-- 进行路由匹配和相关函数调用
local ok = rx:dispatch(uri, opts, opts.matched)
if not ok then
ngx.say("Not Found")
ngx.exit(ngx.HTTP_NOT_FOUND)
end

View File

@ -11,7 +11,7 @@ local systemRole = require("service.system.role")
--定义相关路由前端接口url地址
local routes = {
--用户相关路由接口
--角色相关路由接口
{
paths = { "/api/system/roles" },
methods = { "GET" },

View File

@ -43,7 +43,7 @@ if jwt_obj.verified == false then
end
--判断token是否超时 --令牌已过期
if jwt_obj.payload.exp and os.time() > jwt_obj.payload.exp then
if jwt_obj.payload.exp and ngx.time() > jwt_obj.payload.exp then
ngx.log(ngx.WARN, "token timeout ".. jwt_obj.reason)
ngx.status = ngx.HTTP_UNAUTHORIZED
ngx.exit(ngx.HTTP_UNAUTHORIZED)

View File

@ -84,6 +84,7 @@ function _M:updateSystemAccount(id, jsonData)
if ok == false then
return 0x000001,nil
end
jsonData.update_time = ngx.time()
--对数据内容进行更新
return accountModel:where('id', '=', id):update(jsonData)
end

View File

@ -99,7 +99,8 @@ function _M.updateApplication(id, jsonData)
if ok == false then
return 0x000001,nil
end
--对数据内容进行更新
--对数据内容进行更
jsonData.update_time = ngx.time()
return applicationModel:where('id', '=', id):update(jsonData)
end

View File

@ -20,18 +20,19 @@ local function authenticate(name, passwd)
if passwd == "" then
return 0x010002, nil
end
local pwdMd5 = ngx.md5(passwd)
--根据用户进行验证用户是否存在
local code, res = userModel:where("username", "=", name):where("password", "=", passwd):get()
local code, res = userModel:where("username", "=", name):where("password", "=", pwdMd5):get()
if code == 0 and res ~= nil then
return code, res
end
--根据手机号进行验证用户是否存在
code, res = userModel:where("phone", "=", name):where("password", "=", passwd):get()
code, res = userModel:where("phone", "=", name):where("password", "=", pwdMd5):get()
if code == 0 and res ~= nil then
return code, res
end
--根据邮箱进行验证用户是否存在
code, res = userModel:where("email", "=", name):where("password", "=", passwd):get()
code, res = userModel:where("email", "=", name):where("password", "=", pwdMd5):get()
if code == 0 and res ~= nil then
return code, res
end

View File

@ -83,6 +83,7 @@ function _M.updateSystemDepartment(id, jsonData)
if ok == false then
return 0x000001,nil
end
jsonData.update_time = ngx.time()
--对数据内容进行更新
return departmentModel:where('id', '=', id):update(jsonData)
end

91
src/dao/menu.lua Normal file
View File

@ -0,0 +1,91 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/11/04 15:06
--- 数据表模型文件
local helpers = require("share.helpers")
--引用使用的库文件
local model = require("share.model")
--创建一个数据表相关的模型
local menuModel = model:new('sys_menu')
local _M = {}
--判断菜单是否存在
local function isExistMenu(id)
--根据菜单id进行验证菜单是否存在
local code, res = menuModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
--组织不存在返回错误
if num <= 0 then
return false
end
return true
end
-- 查询数据表中的所有菜单信息
function _M.getSystemMenus(pageNum, pageSize)
return menuModel:paginate(pageNum, pageSize)
end
--根据菜单id获取菜单信息
function _M.getSystemMenu(id)
return menuModel.find(id)
end
--增加菜单息到数据表
function _M.addSystemMenu(jsonData)
--解析json中的键和数据值
local menuid = jsonData['menu_id']
--根据菜单名称进行验证菜单是否存在
local code, res = menuModel:where("menu_id", "=", menuid):get()
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
--键值为id产生uuid数据值增加到json中
jsonData.id = helpers.getUuid()
-- 创建一个菜单
return menuModel:create(jsonData)
end
--删除菜单信息到数据表
function _M.deleteSystemDepartment(id)
--根据菜单id进行验证菜单是否存在
local ok = isExistMenu(id)
--菜单不存在则返回
if ok == false then
return 0x000001,nil
end
return menuModel:delete(id)
end
--更新菜单信息到数据表
function _M.updateSystemMenu(id, jsonData)
--根据菜单id进行验证菜单是否存在
local ok = isExistMenu(id)
--组织不存在则返回
if ok == false then
return 0x000001,nil
end
jsonData.update_time = ngx.time()
--对数据内容进行更新
return menuModel:where('menu_id', '=', id):update(jsonData)
end
return _M

View File

@ -91,6 +91,7 @@ function _M.updateSystemPermission(id, jsonData)
if ok == false then
return 0x000001,nil
end
jsonData.update_time = ngx.time()
--对数据内容进行更新
return permissionModel:where('id', '=', id):update(jsonData)
end

91
src/dao/position.lua Normal file
View File

@ -0,0 +1,91 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/11/04 15:14
--- 数据表模型文件
local helpers = require("share.helpers")
--引用使用的库文件
local model = require("share.model")
--创建一个数据表相关的模型
local positionModel = model:new('sys_post')
local _M = {}
--判断岗位是否存在
local function isExistPosition(id)
--根据岗位id进行验证岗位是否存在
local code, res = positionModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
--岗位不存在返回错误
if num <= 0 then
return false
end
return true
end
-- 查询数据表中的所有岗位信息
function _M.getSystemPositions(pageNum, pageSize)
return positionModel:paginate(pageNum, pageSize)
end
--根据岗位id获取岗位信息
function _M.getSystemPosition(id)
return positionModel.find(id)
end
--增加岗位信息到数据表
function _M.addSystemPosition(jsonData)
--解析json中的键和数据值
local post_id = jsonData['post_id']
--根据岗位id进行验证岗位是否存在
local code, res = positionModel:where("post_id", "=", post_id):get()
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
--键值为id产生uuid数据值增加到json中
jsonData.id = helpers.getUuid()
-- 创建一个岗位
return positionModel:create(jsonData)
end
--删除岗位信息到数据表
function _M.deleteSystemPosition(id)
--根据岗位id进行验证岗位是否存在
local ok = isExistPosition(id)
--岗位不存在则返回
if ok == false then
return 0x000001,nil
end
return positionModel:delete(id)
end
--更新岗位信息到数据表
function _M.updateSystemPosition(id, jsonData)
--根据岗位id进行验证岗位是否存在
local ok = isExistPosition(id)
--岗位不存在则返回
if ok == false then
return 0x000001,nil
end
jsonData.update_time = ngx.time()
--对数据内容进行更新
return positionModel:where('post_id', '=', id):update(jsonData)
end
return _M

View File

@ -84,6 +84,7 @@ function _M:updateSystemRole(id, jsonData)
if ok == false then
return 0x000001,nil
end
jsonData.update_time = ngx.time()
--对数据内容进行更新
return roleModel:where('id', '=', id):update(jsonData)
end

View File

@ -15,7 +15,6 @@ local _M = {}
local user = {
["ID"] = "",
["type"] = 0,
}
--判断用户是否存在
@ -72,6 +71,9 @@ function _M.addSystemUser(jsonData)
--键值为id产生uuid数据值增加到json中
jsonData.id = helpers.getUuid()
--用户密码暂时使用md5进行加密
local pwd = jsonData['password']
jsonData.password = ngx.md5(pwd)
-- 创建一个用户
return userModel:create(jsonData)
end

18
src/init.lua Normal file
View File

@ -0,0 +1,18 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by frankly.
--- DateTime: 2025/11/3 18:44
---
--[[
"ngx_lua""init_by_lua_file";
nginx时初始化一次
--]]
print("init application...")
--初始化获取系统默认的用户权限为实现RBAC框架做权限数据准备
cjson = require "cjson"
local dict_a = ngx.shared.dict_a
local v = dict_a:get("abc")
if not v then
dict_a:set("abc", 9)
end

View File

@ -0,0 +1,76 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/11/04 14:35
--- 业务逻辑 对菜单数据表进行数据表业务处理
local resp = require("util.response")
local menuDao = require("dao.menu")
local validatorJson = require("validator.system.menu")
local cjson = require("cjson.safe")
local _M = {}
--获取所有菜单信息
function _M.getSystemMenus()
--获取页码和请求的数据量
local pageNum = ngx.var.pagenum or 1
local pageSize = ngx.var.pagesize or 10
local code,ret = menuDao.getSystemMenus(pageNum, pageSize)
local result = resp:json(code, ret)
resp:send(result)
end
--根据菜单id获取菜单信息
function _M.getSystemMenu(m)
local code,ret = menuDao.getSystemMenu(m.id)
local result = resp:json(code, ret)
resp:send(result)
end
--根据菜单id添加菜单信息
function _M.addSystemMenu()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = menuDao.addSystemMenu(cjson.decode(body_data))
local result = resp:json(code, ret)
resp:send(result)
end
--根据菜单id删除菜单信息
function _M.deleteSystemMenu(m)
local code, ret = menuDao.deleteSystemMenu(m.id)
local result = resp:json(code, ret)
resp:send(result)
end
--根据菜单id删除菜单信息
function _M.updateSystemMenu(m)
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
local code, ret = menuDao.updateSystemMenu(m.id, cjson.decode(body_data))
local result = resp:json(code, ret)
resp:send(result)
end
return _M

View File

@ -0,0 +1,76 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/11/04 15:01
--- 业务逻辑 对岗位数据表进行数据表业务处理
local resp = require("util.response")
local positionDao = require("dao.position")
local validatorJson = require("validator.system.position")
local cjson = require("cjson.safe")
local _M = {}
--获取所有岗位信息
function _M.getSystemPositions()
--获取页码和请求的数据量
local pageNum = ngx.var.pagenum or 1
local pageSize = ngx.var.pagesize or 10
local code,ret = positionDao.getSystemPositions(pageNum, pageSize)
local result = resp:json(code, ret)
resp:send(result)
end
--根据岗位id获取岗位信息
function _M.getSystemPosition(m)
local code,ret = positionDao.getSystemPosition(m.id)
local result = resp:json(code, ret)
resp:send(result)
end
--根据岗位id添加岗位信息
function _M.addSystemPosition()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = positionDao.addSystemPosition(cjson.decode(body_data))
local result = resp:json(code, ret)
resp:send(result)
end
--根据岗位id删除岗位信息
function _M.deleteSystemPosition(m)
local code, ret = positionDao.deleteSystemPosition(m.id)
local result = resp:json(code, ret)
resp:send(result)
end
--根据岗位id删除岗位信息
function _M.updateSystemPosition(m)
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
local code, ret = positionDao.updateSystemPosition(m.id, cjson.decode(body_data))
local result = resp:json(code, ret)
resp:send(result)
end
return _M

View File

@ -7,9 +7,22 @@ local resp = require("util.response")
local userDao = require("dao.user")
local validatorJson = require("validator.system.user")
local cjson = require("cjson.safe")
local token = require("util.token")
local _M = {}
--验证用户id与token中的用户id是否一致
local function getUserId()
--获取请求头中的令牌数据
local auth_header = ngx.var.http_Authorization
--验证数据的正确性
local retToken = token.authorizationToken(auth_header)
--token前面已经进行验证不需要进行判断
--验证成功获取用户id信息
local userid = retToken["body"]["payload"]["userid"]
return userid
end
--获取所有用户信息
function _M.getSystemUsers()
--获取页码和请求的数据量
@ -23,6 +36,12 @@ end
--根据用户id获取用户信息
function _M.getSystemUser(m)
local userid = getUserId()
if userid ~= m.id then
ngx.log(ngx.WARN, "用户与使用token中的用户id不一致")
ngx.status = ngx.HTTP_NOT_ALLOWED
ngx.exit(ngx.HTTP_NOT_ALLOWED)
end
local code,ret = userDao.getSystemUser(m.id)
local result = resp:json(code, ret)
resp:send(result)
@ -59,6 +78,12 @@ end
--根据用户id删除用户信息
function _M.updateSystemUser(m)
local userid = getUserId()
if userid ~= m.id then
ngx.log(ngx.WARN, "用户与使用token中的用户id不一致")
ngx.status = ngx.HTTP_NOT_ALLOWED
ngx.exit(ngx.HTTP_NOT_ALLOWED)
end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据

Binary file not shown.

View File

@ -127,23 +127,23 @@ end
--获取当前时间戳(毫秒)
function snowflake:getCurrentTimestamp()
local timestamp = os.time()
local timestamp = ngx.time()
return timestamp
end
--获取新的时间戳
function snowflake:getNextTimestamp(lastTimestamp)
local timestamp = math.floor(os.time());
local timestamp = math.floor(ngx.time());
while (timestamp <= lastTimestamp)
do
timestamp = math.floor(os.time());
timestamp = math.floor(ngx.time());
end
return timestamp;
end
-- 雪花算法的实现
function snowflake:generateUniqueId()
--local curtime = os.time()
--local curtime = ngx.time()
--print("current time: ", curtime)
local timestamp = self.getCurrentTimestamp() -- 当前时间戳(毫秒)
-- 如果是同一时间生成的,则进行毫秒内序列

View File

@ -7,7 +7,7 @@
local helpers = require("share.helpers")
local jsonschema = require("jsonschema")
local cjson = require("cjson.safe")
--
--local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
--local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
--local snow = snowflake.new(workerId, datacenterId)
@ -16,6 +16,9 @@ local cjson = require("cjson.safe")
--max =a and b or c--a?b:c
local mylib = require "addlib"
ngx.say(addlib.add(5,7))
--[[
local uuid = require("resty.jit-uuid")
uuid.seed()
@ -33,6 +36,7 @@ local pageSize = args["pagesize"] or 10
ngx.say("pageNum:", pageNum, " pageSize:", pageSize)
--]]
--[[
local schema = {
type = 'object',
properties = {
@ -53,6 +57,7 @@ if not result then
end
local token = string.sub(auth_header,8)
ngx.say(token)
--]]
--local sampleJson = [[{"raw_header":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9","signature":"zkKAmXifqWDrMaLpXe8hrA1JpDRbdlgwS-yxNnQUOBw","raw_payload":"eyJpYXQiOjE3NjE4OTIwNDMsImV4cCI6MTc2MTg5NTY0MywidXNlcmlkIjoiYWRtaW4iLCJyb2xlIjoiIn0","valid":true,"verified":true,"reason":"everything is awesome~ :p","header":{"alg":"HS256","typ":"JWT"},"payload":{"iat":1761892043,"userid":"admin","exp":1761895643,"role":""}}]]
----解析json字符串

View File

@ -26,7 +26,7 @@ permission_system:assign_role("user002", "user_manager")
permission_system:assign_role("admin001", "super_admin")
-- 测试权限验证
print("=== RBAC权限验证测试 ===")
ngx.say("=== RBAC权限验证测试 ===")
-- 测试用户001guest角色
local test_cases = {
@ -46,17 +46,17 @@ local test_cases = {
for _, test in ipairs(test_cases) do
local result = permission_system:check_permission(test.user_id, test.resource, test.action)
local status = result == test.expected and "✓ 通过" or "✗ 失败"
print(string.format("%s 用户:%s 资源:%s 方法:%s 结果:%s",
ngx.say(string.format("%s 用户:%s 资源:%s 方法:%s 结果:%s",
status, test.user_id, test.resource, test.action, tostring(result)))
end
-- 显示用户权限列表
print("\n=== 用户权限列表 ===")
ngx.say("\n=== 用户权限列表 ===")
local users = {"user001", "user002", "admin001"}
for _, user_id in ipairs(users) do
local permissions = permission_system:get_user_permissions(user_id)
print(string.format("用户 %s 的权限:", user_id))
ngx.say(string.format("用户 %s 的权限:", user_id))
for _, perm in ipairs(permissions) do
print(string.format(" - %s %s", perm.action, perm.resource))
ngx.say(string.format(" - %s %s", perm.action, perm.resource))
end
end

View File

@ -16,7 +16,7 @@ function _M:json(status, message, data, http_status)
--end
msg = error_code[status]
end
local response = {code=status, msg=msg, result=data,timestamp=os.time()}
local response = {code=status, msg=msg, result=data,timestamp=ngx.time()}
if not response.code then
response.code = -1
response.message = 'not find status code'
@ -34,7 +34,7 @@ function _M:json(status, data, http_status)
local response_status = http_status or ngx.OK
msg = error_code[status]
local response = {code=status, msg=msg, result=data,timestamp=os.time()}
local response = {code=status, msg=msg, result=data,timestamp=ngx.time()}
if not response.code then
response.code = -1
response.message = 'not find status code'
@ -51,7 +51,7 @@ function _M:raw(http_status, http_body)
code = http_status,
headers = {},
body = http_body,
timestamp = os.time()
timestamp = ngx.time()
}
end

View File

@ -26,8 +26,8 @@ local obj = {
role = "", -- 角色
--iss = "your_issuer", -- 签发者
--sub = "1234567890", -- 主题
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时
iat = os.time() -- 签发时间
exp = ngx.time() + 3600, -- 过期时间(例如:当前时间+1小时
iat = ngx.time() -- 签发时间
}
}
@ -76,7 +76,7 @@ function _M.authorizationToken(auth_header)
return response
end
--判断token是否超时
if jwt_obj.payload.exp and os.time() > jwt_obj.payload.exp then
if jwt_obj.payload.exp and ngx.time() > jwt_obj.payload.exp then
response["code"] = 401
response["message"] = "令牌已过期"
return response

View File

@ -0,0 +1,37 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/11/04 11:12
--- 业务逻辑 对菜单数据进行参数数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "menu_id", type = "string"},
{name = "menu_name", type = "string"},
{name = "parent_id", type = "string"},
{name = "order_num", type = "number"},
{name = "url", type = "string"},
{name = "target", type = "string"},
{name = "menu_type", type = "string"},
{name = "status", type = "string"},
{name = "is_refresh", type = "string"},
{name = "perms", type = "string"},
{name = "perms", type = "string"},
{name = "create_by", type = "string"},
{name = "update_by", type = "string"},
{name = "remark", type = "string"},
}, required = {"menu_id", "menu_name"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)
return result
end
return _M

View File

@ -0,0 +1,30 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/11/04 11:20
--- 业务逻辑 对岗位数据进行参数数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "post_id", type = "string"},
{name = "post_code", type = "string"},
{name = "post_name", type = "string"},
{name = "post_sort", type = "number"},
{name = "status", type = "string"},
{name = "create_by", type = "string"},
{name = "update_by", type = "string"},
}, required = {"post_id"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)
return result
end
return _M