-- post.lua
ngx.req.read_body()
local args, err = ngx.req.get_post_args()
if not args then
ngx.status = 400
ngx.say("Bad Request: ", err)
ngx.exit(ngx.HTTP_BAD_REQUEST)
end
local post = {
title = args.title,
content = args.content
}
-- posts.lua
local json = require "cjson"
local posts = {
{ title = "Post 1", content = "Content of post 1" },
{ title = "Post 2", content = "Content of post 2" },
}
ngx.say('{"posts": ', json.encode(posts), '}')
local aes = require "resty.aes"
local ui = "lEpl%2BBt4U2lgKDJnnehI2ynsG4B%2FNcP%2FVK7fmYXdw7QfC5y2CwAZ%2BZN21QIpQ6kzdSk%2BLH2gYsULveGpdRF7e59b2wgcYQ74F1lmU84EF%2FA%3D"
local decoded = ngx.decode_base64(ngx.unescape_uri(ui))
local decodedLength = string.len(decoded)
local iv = string.sub(decoded, -16, decodedLength)
local aes_256_cbc_md5 = aes:new("your password", nil, aes.cipher(256, "cbc"), {iv=iv})
local decrypted = aes_256_cbc_md5:decrypt(decoded)
local ck, err = cookie:new()
if not ck then
ngx.say(ngx.ERR, err)
end
local ns, err = ck:get("ns")
设置 Cookie 示例如下:
local function set_cookie(name, value, max_age)
local cookie = require "resty.cookie"
local ck = cookie:new()
local ok, err = ck:set({
key = name,
value = value,
path = "/",
max_age = max_age,
secure = false,
httponly = true
})
if not ok then
ngx.log(ngx.ERR, "Failed to set cookie: ", err)
return false
end
return true
end
清除 Cookie:
local function clear_cookie(name)
local expires = "Expires=Thu, 01 Jan 1970 00:00:01 GMT;"
local path = "Path=/;"
local cookie = string.format("%s=; %s %s", name, expires, path)
ngx.header["Set-Cookie"] = cookie
end