OpenResty 操作cookies
在or中简单的使用cookies 复杂的操作请使用
[lua_resty_cookies](https://github.com/cloudflare/lua-resty-cookie)
基本操作
- 获取一个cookies, cookies的name叫做session
local cookie_name = "cookie_session"
ngx.say(ngx.var[cookie_name])
- 设置 cookies
ngx.header["Set-Cookie"] = "session=af54; Path=/; Expires=" ..
ngx.cookie_time(ngx.time() + 86400)
测试代码
location ~ /header_add {
content_by_lua_block {
ngx.header["Set-Cookie"] = "session=lzz; Path=/; Expires=" ..
ngx.cookie_time(ngx.time() + 86400)
ngx.say("orangleliu")
}
}
看看效果
# curl -i 127.0.0.1:8001/header_add
HTTP/1.1 200 OK
Server: openresty/1.9.7.1
Date: Fri, 16 Dec 2016 07:00:30 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: session=lzz; Path=/; Expires=Sat, 17-Dec-16 07:00:30 GMT
- set cookie 使用的是直接操作 header 的方式,这个时候如果单独添加一个cookie就会覆盖掉其他的值,所以需要多做一些处理。
function get_cookies()
local cookies = ngx.header["Set-Cookie"] or {}
if type(cookies) == "string" then
cookies = {cookies}
end
return cookies
end
function add_cookie(cookie)
local cookies = get_cookies()
table.insert(cookies, cookie)
ngx.header['Set-Cookie'] = cookies
end
add_cookie("session=aff12d; Path=/")
⚠️:上面的代码需要在 header_filter 阶段运行,在这个阶段可以获取 应用或者是后端 proxy 设置的cookies, 如果你想要 添加,删除,替换某一个cookies,注意不要覆盖所有的 cookies了。
- 删除cookies跟上面类似
一个例子,在content阶段设置了多个cookies,然后在 header_filter 阶段删除cookies
location ~ /header_delete {
content_by_lua_block {
ngx.header["Set-Cookie"] = {'Foo=abc; path=/', 'age=18; path=/'}
ngx.say("openresty")
}
header_filter_by_lua_block {
local match = string.match
local function get_cookies()
-- local cookies = ngx.header["Set-Cookie"] or {}
local cookies = ngx.header.set_cookie or {}
if type(cookies) == "string" then
cookies = {cookies}
end
return cookies
end
local function remove_cookie(cookie_name)
local cookies = get_cookies()
ngx.log(ngx.ERR, "source cookies ", table.concat(cookies, " "))
for key, value in ipairs(cookies) do
local name = match(value, "(.-)=")
ngx.log(ngx.ERR, key.."<=>", value)
if name == cookie_name then
table.remove(cookies, key)
end
end
ngx.header['Set-Cookie'] = cookies or {}
ngx.log(ngx.ERR, "new cookies ", table.concat(cookies, " "))
end
remove_cookie("Foo")
}
}
}
测试下
# curl '127.0.0.1:8001/header_delete' -i
HTTP/1.1 200 OK
Server: openresty/1.9.7.1
Date: Fri, 16 Dec 2016 08:07:34 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: age=18; path=/
其他需求
- 在做 proxy_pass 之后想给请求统一修改cookies值这么做呢?反正都很类似了。。
使用 header_filter_by_lua 指令
header_filter_by_lua '
local cookies = ngx.header.set_cookie
if cookies then
if type(cookies) ~= "table" then
cookies = {cookies}
end
local gsub = string.gsub
local changed
for i, cookie in ipairs(cookies) do
local new_cookie = gsub(cookie, "^target%-cookie=[^;]*", "target-cookie=myvalue", 1)
if new_cookie ~= cookie then
cookies[i] = new_cookie
changed = true
end
end
if changed then
ngx.header.set_cookie = cookies
end
end
';
https://github.com/openresty/headers-more-nginx-module/issues/18
ref
OpenResty 操作cookies的更多相关文章
- Openresty 操作Cookie
Openresty 操作cookie共有两种方法: 1.直接操作 1.1 获取Cookie 获取所有cookie: ngx.var.http_cookie, 这里获取的是一个字符串,如果不存在则返回n ...
- JS操作Cookies
JS操作Cookies 获取Cookie function getCookie(c_name) { if (document.cookie.length > 0) { c_start = doc ...
- Selenium常用操作汇总二——如何操作cookies(转)
Web 测试中我们经常会接触到Cookies,一个Cookies主要属性有”所在域.name.value.有效日期和路径",下面来讲一下怎么操作Cookies. import java.ut ...
- JS操作Cookies的小例子
这篇文章介绍了JS操作Cookies的小例子,有需要的朋友可以参考一下. 您可能感兴趣的文章:js 保存与获取cookie的代码javascript cookie操作实例详解javascript co ...
- javascript 操作cookies详解
javascript 操作cookies详解 这段操作cookies的方法我使用很久了,但是一直一来没遇到什么问题,今天在做一个在第一个页面保存了cookies,第二个页面获取或者第三个页面获取的功能 ...
- selenium webdriver学习(九)------------如何操作cookies(转)
selenium webdriver学习(九)------------如何操作cookies 博客分类: Selenium-webdriver Web 测试中我们经常会接触到Cookies,一个C ...
- selenium如何操作cookies实现免登录
执行接口测试或者某些自动化测试时,为了避免每次访问接口都需要登录操作,可以用访问接口时,把cookies信息传过去. 思路是先登录一次页面,获取到cookies信息,把cookies信息保存到本地文件 ...
- 使用jQuery操作Cookies的实现代码
Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是 ...
- java操作cookies
建立一个无生命周期的cookie,即随着浏览器的关闭即消失的cookie,代码如下 HttpServletRequest request HttpServletResponse response Co ...
随机推荐
- CSS Box Model 盒子模型
1. 介绍 1.1 什么是 Box Model 在HTML中的每个element(元素)都可以看作一个矩形的盒子,矩形从内到外依次由元素的内容(content).内边距(padding).边框(bor ...
- 原生js中实现全选和反选功能
<!DOCTYPE html> <html> <head lang="en"> <meta char ...
- Python系列之 - python循环语句
前两篇说的是数据类型和数据运算,本篇来讲讲条件语句和循环语句. 1. 条件语句 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执 ...
- Efficient&Elegant:Java程序员入门Cpp
最近项目急需C++ 的知识结构,虽说我有过快速学习很多新语言的经验,但对于C++ 老特工我还需保持敬畏(内容太多),本文会从一个Java程序员的角度,制定高效学习路线快速入门C++ . Java是为了 ...
- 学习flexible.js
flexible是手淘团队总结的一套移动端适配方案,这篇文章是自己对flexible学习历程的记录,也算是一个备忘. https://github.com/amfe/article/issues/17 ...
- Discuz!另一处SSRF无须登陆无须条件
漏洞来源:http://wooyun.jozxing.cc/static/bugs/wooyun-2015-0151179.html 看看poc:http://phpstudy.com/Discuz_ ...
- [Codeforces 864A]Fair Game
Description Petya and Vasya decided to play a game. They have n cards (n is an even number). A singl ...
- codeforces 842C Ilya And The Tree
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very ...
- 灰狼呼唤着同胞(brethren)
先求出确定边的联通块,有cnt块,显然方案数为2^(cnt-1) 联通块用dfs很好求 但此题还有并查集解法,且与一道叫团伙的题很像 边为0为敌人,1为朋友,敌人的敌人是朋友,朋友的朋友是朋友,正好对 ...
- 【LSGDOJ 2015】数页码
题目描述 一本书的页码是从 1-n 编号的连续整数:1, 2, 3, ... , n.请你求出全部页码中所有单个数字的和,例如第 123 页,它的和就是 1+2+3=6. 输入 一行为 n(1 < ...