修改oauth登录后返回code,并将code存储到共享字典中,设置有效时间进行自动删除
This commit is contained in:
parent
242391f577
commit
2c525e1f61
|
|
@ -26,7 +26,7 @@ http {
|
|||
|
||||
#在Nginx启动时执行的Lua代码块
|
||||
#初始化用户角色权限相关的共享内存
|
||||
lua_shared_dict dict 10m;
|
||||
lua_shared_dict codeDict 5m;
|
||||
#init_by_lua_block {
|
||||
# -- 定义一个全局变量
|
||||
# ngx.log(ngx.INFO, "Initializing global variable")
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ local routes = {
|
|||
{
|
||||
paths = { "/api/oauth/v2/login" },
|
||||
methods = { "POST" },
|
||||
handler = oauthService.userinfo,
|
||||
handler = oauthService.login,
|
||||
},
|
||||
--根据Access-Token获取相应用户的账户信息
|
||||
{
|
||||
|
|
|
|||
|
|
@ -171,6 +171,36 @@ function _M:token()
|
|||
}))
|
||||
end
|
||||
|
||||
--用户进行登陆然后验证返回code
|
||||
function _M:login()
|
||||
--读取请求体的数据
|
||||
ngx.req.read_body()
|
||||
--获取请求数据
|
||||
local body_data = ngx.req.get_body_data()
|
||||
-- 验证数据是否符合json
|
||||
local ok = validator.validatorLogin(body_data)
|
||||
--验证失败则返回
|
||||
if not ok then
|
||||
local result = resp:json(0x000001)
|
||||
resp:send(result)
|
||||
return
|
||||
end
|
||||
|
||||
--用户验证成功后,返回code值:用户id当前时间和随机数进行md5后生存一个code
|
||||
local user_id = "11"
|
||||
local current_time = ngx.time()
|
||||
local code = ngx.md5(user_id..current_time..math.random())
|
||||
|
||||
--将code放入到共享内存中
|
||||
local key = user_id.."-code"
|
||||
local shared_dict = ngx.shared.codeDict
|
||||
shared_dict:set(key, code)
|
||||
shared_dict:expire(key, 10)
|
||||
--发送code到前端请求
|
||||
local result = resp:json(0, code)
|
||||
resp:send(result)
|
||||
end
|
||||
|
||||
--根据Access-Token获取相应用户的账户信息
|
||||
function _M:userinfo()
|
||||
--读取请求体的数据
|
||||
|
|
@ -178,7 +208,7 @@ function _M:userinfo()
|
|||
--获取请求数据
|
||||
local body_data = ngx.req.get_body_data()
|
||||
-- 验证数据是否符合json
|
||||
local ok = validator.validatorJson(body_data)
|
||||
local ok = validator.validatorUserinfo(body_data)
|
||||
--验证失败则返回
|
||||
if not ok then
|
||||
local result = resp:json(0x000001)
|
||||
|
|
@ -194,13 +224,27 @@ function _M:logout()
|
|||
--获取请求数据
|
||||
local body_data = ngx.req.get_body_data()
|
||||
-- 验证数据是否符合json
|
||||
local ok = validator.validatorJson(body_data)
|
||||
local ok = validator.validatorLogout(body_data)
|
||||
--验证失败则返回
|
||||
if not ok then
|
||||
local result = resp:json(0x000001)
|
||||
resp:send(result)
|
||||
return
|
||||
end
|
||||
|
||||
--用户验证成功后,返回code值:用户id当前时间和随机数进行md5后生存一个code
|
||||
local user_id = "11"
|
||||
local current_time = ngx.time()
|
||||
local code = ngx.md5(user_id..current_time..math.random())
|
||||
|
||||
--将code放入到共享内存中
|
||||
local key = user_id.."-code"
|
||||
local shared_dict = ngx.shared.codeDict
|
||||
shared_dict:set(key, code)
|
||||
shared_dict:expire(key, 10)
|
||||
--发送code到前端请求
|
||||
local result = resp:json(0, code)
|
||||
resp:send(result)
|
||||
end
|
||||
|
||||
--根据Refresh-Token刷新Access-Token
|
||||
|
|
@ -210,7 +254,7 @@ function _M:refresh()
|
|||
--获取请求数据
|
||||
local body_data = ngx.req.get_body_data()
|
||||
-- 验证数据是否符合json
|
||||
local ok = validator.validatorJson(body_data)
|
||||
local ok = validator.validatorRefresh(body_data)
|
||||
--验证失败则返回
|
||||
if not ok then
|
||||
local result = resp:json(0x000001)
|
||||
|
|
@ -226,7 +270,7 @@ function _M:checklogin()
|
|||
--获取请求数据
|
||||
local body_data = ngx.req.get_body_data()
|
||||
-- 验证数据是否符合json
|
||||
local ok = validator.validatorJson(body_data)
|
||||
local ok = validator.validatorLogout(body_data)
|
||||
--验证失败则返回
|
||||
if not ok then
|
||||
local result = resp:json(0x000001)
|
||||
|
|
|
|||
|
|
@ -47,6 +47,13 @@ local x509 = require "resty.openssl.x509"
|
|||
--local pem = require "resty.openssl.pem"
|
||||
--local rand = require "resty.openssl.rand"
|
||||
|
||||
local key = "11-code"
|
||||
local shared_dict = ngx.shared.codeDict
|
||||
local codeV = shared_dict:get(key)
|
||||
if codeV ~= nil then
|
||||
ngx.say("code valus:".. codeV)
|
||||
end
|
||||
|
||||
local user_id = "11"
|
||||
local client_id = "aaaasddd"
|
||||
local current_time = ngx.time()
|
||||
|
|
|
|||
|
|
@ -43,6 +43,21 @@ function _M:validatorToken(jsonData)
|
|||
return result
|
||||
end
|
||||
|
||||
local schemaLogin = {
|
||||
{type = "object", properties = {
|
||||
{name = "username", type = "string"},
|
||||
{name = "password", type = "string"},
|
||||
}, required = {"username", "password"}}
|
||||
}
|
||||
|
||||
--回收Access-Token
|
||||
function _M:validatorLogin(jsonData)
|
||||
-- 验证数据是否符合schema
|
||||
local validator = jsonschema.generate_validator(schemaLogin)
|
||||
local result = validator(jsonData)
|
||||
return result
|
||||
end
|
||||
|
||||
local schemaUserInfo = {
|
||||
{type = "object", properties = {
|
||||
{name = "username", type = "string"},
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user