增加日志登录后将用户ip地址写入到日志中,token中需要beare 形式,用户信息校验先增加部分大小认证
This commit is contained in:
parent
6b3f93dfd9
commit
64b4e82145
|
|
@ -2,6 +2,13 @@ local jwt = require "resty.jwt"
|
||||||
local validators = require "resty.jwt-validators"
|
local validators = require "resty.jwt-validators"
|
||||||
local conf = require("config")
|
local conf = require("config")
|
||||||
|
|
||||||
|
-- 定义一个JSON Schema
|
||||||
|
local schema = {
|
||||||
|
{type = "object", properties = {
|
||||||
|
{name = "username", type = "string", minLength = 8, maxLength = 20},
|
||||||
|
}, required = {"username", "phone", "email", "idcard"}}
|
||||||
|
}
|
||||||
|
|
||||||
--获取用户认证数据信息
|
--获取用户认证数据信息
|
||||||
local auth_header = ngx.var.http_Authorization
|
local auth_header = ngx.var.http_Authorization
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,9 @@ local _M = {}
|
||||||
|
|
||||||
--用户登录业务逻辑处理
|
--用户登录业务逻辑处理
|
||||||
function _M.login()
|
function _M.login()
|
||||||
|
--获取远端客户端的IP地址
|
||||||
|
local client_ip = ngx.var.remote_addr
|
||||||
|
ngx.log(ngx.INFO, "client_ip:"..client_ip.." login system")
|
||||||
--读取请求体的数据
|
--读取请求体的数据
|
||||||
ngx.req.read_body()
|
ngx.req.read_body()
|
||||||
--获取请求数据
|
--获取请求数据
|
||||||
|
|
@ -41,7 +44,6 @@ function _M.login()
|
||||||
end
|
end
|
||||||
|
|
||||||
--获取的登陆的用户信息,返回tocken
|
--获取的登陆的用户信息,返回tocken
|
||||||
--ngx.log(ngx.INFO, "userid:"..id.." username:"..username)
|
|
||||||
local jwt_token = token.generateToken(id, username)
|
local jwt_token = token.generateToken(id, username)
|
||||||
local data = {}
|
local data = {}
|
||||||
data["token"] = jwt_token
|
data["token"] = jwt_token
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,44 @@ else
|
||||||
end
|
end
|
||||||
--]]
|
--]]
|
||||||
|
|
||||||
|
--用于接收前端数据的对象
|
||||||
|
local args=nil
|
||||||
|
--获取前端的请求方式 并获取传递的参数
|
||||||
|
local request_method = ngx.var.request_method
|
||||||
|
--判断是get请求还是post请求并分别拿出相应的数据
|
||||||
|
if"GET" == request_method then
|
||||||
|
args = ngx.req.get_uri_args()
|
||||||
|
elseif "POST" == request_method then
|
||||||
|
ngx.req.read_body()
|
||||||
|
args = ngx.req.get_post_args()
|
||||||
|
--兼容请求使用post请求,但是传参以get方式传造成的无法获取到数据的bug
|
||||||
|
if (args == nil or args.data == null) then
|
||||||
|
args = ngx.req.get_uri_args()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--获取前端传递的name值
|
||||||
|
local name =
|
||||||
|
--响应前端
|
||||||
|
ngx.say("linux hello:"..name)
|
||||||
|
|
||||||
|
--[[
|
||||||
|
local M = {}
|
||||||
|
local charset = {} do -- [0-9a-zA-Z]
|
||||||
|
for c = 48, 57 do table.insert(charset, string.char(c)) end
|
||||||
|
for c = 65, 90 do table.insert(charset, string.char(c)) end
|
||||||
|
for c = 97, 122 do table.insert(charset, string.char(c)) end
|
||||||
|
end
|
||||||
|
function M.uuid(length)
|
||||||
|
local res = ""
|
||||||
|
for i = 1, length do
|
||||||
|
res = res .. charset[math.random(1, #charset)]
|
||||||
|
end
|
||||||
|
return res
|
||||||
|
end
|
||||||
|
return M
|
||||||
|
--]]
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
local jwt = require("resty.jwt")
|
local jwt = require("resty.jwt")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ function _M.generateToken(userid, username)
|
||||||
obj.payload.username = username
|
obj.payload.username = username
|
||||||
--获取的登陆的用户信息,返回tocken
|
--获取的登陆的用户信息,返回tocken
|
||||||
local jwt_token = jwt:sign(conf.secret_key, obj)
|
local jwt_token = jwt:sign(conf.secret_key, obj)
|
||||||
return jwt_token
|
return "Bearer "..jwt_token
|
||||||
end
|
end
|
||||||
|
|
||||||
--令牌校验
|
--令牌校验
|
||||||
|
|
@ -45,7 +45,7 @@ function _M.authorizationToken(auth_header)
|
||||||
response["message"] = "没有找到令牌数据"
|
response["message"] = "没有找到令牌数据"
|
||||||
return response
|
return response
|
||||||
end
|
end
|
||||||
--[[
|
|
||||||
--查找令牌中的Bearer前缀字符,并进行截取
|
--查找令牌中的Bearer前缀字符,并进行截取
|
||||||
local _, _, token = string.find(auth_header, "Bearer%s+(.+)")
|
local _, _, token = string.find(auth_header, "Bearer%s+(.+)")
|
||||||
--如果没有Bearer,则表示令牌无效
|
--如果没有Bearer,则表示令牌无效
|
||||||
|
|
@ -54,7 +54,7 @@ function _M.authorizationToken(auth_header)
|
||||||
response["message"] = "令牌格式不正确"
|
response["message"] = "令牌格式不正确"
|
||||||
return response
|
return response
|
||||||
end
|
end
|
||||||
--]]
|
|
||||||
--校验令牌
|
--校验令牌
|
||||||
local jwt_obj = jwt:verify(conf.secret_key, auth_header)
|
local jwt_obj = jwt:verify(conf.secret_key, auth_header)
|
||||||
--如果校验结果中的verified==false,则表示令牌无效
|
--如果校验结果中的verified==false,则表示令牌无效
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,13 @@ local _M = {}
|
||||||
-- 定义一个JSON Schema
|
-- 定义一个JSON Schema
|
||||||
local schema = {
|
local schema = {
|
||||||
{type = "object", properties = {
|
{type = "object", properties = {
|
||||||
{name = "username", type = "string"},
|
{name = "username", type = "string", minLength = 8, maxLength = 20},
|
||||||
{name = "phone", type = "string"},
|
{name = "phone", type = "string",minLength = 11},
|
||||||
{name = "email", type = "string"},
|
{name = "email", type = "string"},
|
||||||
{name = "idcard", type = "string"},
|
{name = "idcard", type = "string"},
|
||||||
{name = "name", type = "string"},
|
{name = "name", type = "string"},
|
||||||
{name = "office_phone", type = "string"},
|
{name = "office_phone", type = "string"},
|
||||||
{name = "telephone", type = "string"},
|
{name = "telephone", type = "string",minLength = 11},
|
||||||
{name = "display_name", type = "string"},
|
{name = "display_name", type = "string"},
|
||||||
}, required = {"username", "phone", "email", "idcard"}}
|
}, required = {"username", "phone", "email", "idcard"}}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user