摘要:鏈接需求根據(jù)的和決定是否緩存請求比如里然后請求如果和一樣,則緩存,否則不緩存解決方案使用基于的高性能緩存服務(wù)器
鏈接 https://stackoverflow.com/que...
需求根據(jù)JWT的key和URL決定是否緩存HTTP請求
比如JWT里
payload: { "iss": "iss", "sub": "sub", "userGroupID": "{userGroupID}" }
然后請求 https://myapi.example.com/gro...{groupID}/cars
如果 userGroupID和groupID一樣,則緩存,否則不緩存
解決方案使用 https://github.com/jiangwenyu... 基于HAProxy的高性能緩存服務(wù)器
1. Download and build, you will need lua
wget https://github.com/jiangwenyuan/nuster/releases/download/v1.8.8.2/nuster-1.8.8.2.tar.gz make TARGET=linux2628 USE_ZLIB=1 USE_OPENSSL=1 USE_LUA=1 LUA_LIB=/opt/lua-5.3.1/lib LUA_INC=/opt/lua-5.3.1/include
2. create lua script, say, jwt_group_match.lua
-- base64 FROM http://lua-users.org/wiki/BaseSixtyFour local b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" function dec(data) data = string.gsub(data, "[^"..b.."=]", "") return (data:gsub(".", function(x) if (x == "=") then return "" end local r,f="",(b:find(x)-1) for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and "1" or "0") end return r; end):gsub("%d%d%d?%d?%d?%d?%d?%d?", function(x) if (#x ~= 8) then return "" end local c=0 for i=1,8 do c=c+(x:sub(i,i)=="1" and 2^(8-i) or 0) end return string.char(c) end)) end -- end base64 -- json FROM https://gist.github.com/tylerneylon/59f4bcf316be525b30ab json = {} function kind_of(obj) if type(obj) ~= "table" then return type(obj) end local i = 1 for _ in pairs(obj) do if obj[i] ~= nil then i = i + 1 else return "table" end end if i == 1 then return "table" else return "array" end end function escape_str(s) local in_char = {"", """, "/", "", "f", " ", " ", " "} local out_char = {"", """, "/", "b", "f", "n", "r", "t"} for i, c in ipairs(in_char) do s = s:gsub(c, "" .. out_char[i]) end return s end function skip_delim(str, pos, delim, err_if_missing) pos = pos + #str:match("^%s*", pos) if str:sub(pos, pos) ~= delim then if err_if_missing then error("Expected " .. delim .. " near position " .. pos) end return pos, false end return pos + 1, true end function parse_str_val(str, pos, val) val = val or "" local early_end_error = "End of input found while parsing string." if pos > #str then error(early_end_error) end local c = str:sub(pos, pos) if c == """ then return val, pos + 1 end if c ~= "" then return parse_str_val(str, pos + 1, val .. c) end -- We must have a character. local esc_map = {b = "", f = "f", n = " ", r = " ", t = " "} local nextc = str:sub(pos + 1, pos + 1) if not nextc then error(early_end_error) end return parse_str_val(str, pos + 2, val .. (esc_map[nextc] or nextc)) end function parse_num_val(str, pos) local num_str = str:match("^-?%d+%.?%d*[eE]?[+-]?%d*", pos) local val = tonumber(num_str) if not val then error("Error parsing number at position " .. pos .. ".") end return val, pos + #num_str end json.null = {} -- This is a one-off table to represent the null value. function json.parse(str, pos, end_delim) pos = pos or 1 if pos > #str then error("Reached unexpected end of input.") end local pos = pos + #str:match("^%s*", pos) -- Skip whitespace. local first = str:sub(pos, pos) if first == "{" then -- Parse an object. local obj, key, delim_found = {}, true, true pos = pos + 1 while true do key, pos = json.parse(str, pos, "}") if key == nil then return obj, pos end if not delim_found then error("Comma missing between object items.") end pos = skip_delim(str, pos, ":", true) -- true -> error if missing. obj[key], pos = json.parse(str, pos) pos, delim_found = skip_delim(str, pos, ",") end elseif first == "[" then -- Parse an array. local arr, val, delim_found = {}, true, true pos = pos + 1 while true do val, pos = json.parse(str, pos, "]") if val == nil then return arr, pos end if not delim_found then error("Comma missing between array items.") end arr[#arr + 1] = val pos, delim_found = skip_delim(str, pos, ",") end elseif first == """ then -- Parse a string. return parse_str_val(str, pos + 1) elseif first == "-" or first:match("%d") then -- Parse a number. return parse_num_val(str, pos) elseif first == end_delim then -- End of an object or array. return nil, pos + 1 else -- Parse true, false, or null. local literals = {["true"] = true, ["false"] = false, ["null"] = json.null} for lit_str, lit_val in pairs(literals) do local lit_end = pos + #lit_str - 1 if str:sub(pos, lit_end) == lit_str then return lit_val, lit_end + 1 end end local pos_info_str = "position " .. pos .. ": " .. str:sub(pos, pos + 10) error("Invalid json syntax starting at " .. pos_info_str) end end -- end json -- nuster jwt function jwt_group_match(txn) local hdr = txn.http:req_get_headers() local jwt = hdr["jwt"] if jwt == nil then return false end _, payload, _ = jwt[0]:match"([^.]*)%.([^.]*)%.(.*)" if payload == nil then return false end local payload_dec = dec(payload) local payload_json = json.parse(payload_dec) if txn.sf:path() == "/group/" .. payload_json["userGroupID"] .. "/cars" then return true end return false end core.register_fetches("jwt_group_match", jwt_group_match)
3. create conf, say, nuster.conf
global nuster cache on dict-size 1m data-size 100m debug lua-load jwt_group_match.lua frontend web1 bind *:8080 mode http default_backend app1 backend app1 mode http http-request set-var(req.jwt_group_match) lua.jwt_group_match nuster cache on nuster rule group if { var(req.jwt_group_match) -m bool } server s1 127.0.0.1:8000 server s2 127.0.0.1:8001
3. start nuster
./haproxy -f nuster.conf
TEST
payload: { "iss": "iss", "sub": "sub", "userGroupID": "nuster" } curl http://127.0.0.1:8080/group/nuster/cars --header "jwt: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJpc3MiLCJzdWIiOiJzdWIiLCJ1c2VyR3JvdXBJRCI6Im51c3RlciJ9.hPpqQS0d4T2BQP90ZDcgxnqJ0AHmwWFqZvdxu65X3FM"
First run
[CACHE] To create
Second run
[CACHE] Hit
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/61986.html
摘要:請注意閉包的返回值必需是。開發(fā)者可以利用相關(guān)方法來自定義其內(nèi)容,會將閉包的返回值作為最終結(jié)果與其他后端服務(wù)的響應(yīng)合并,然后返回給訪問層。注意不要忘記本身是一個閉包否則,無法模擬過期后重新生成另一個的情況。 dgate:an API Gateway based on Vert.x dgate是基于Vertx的API Gateway。運行dgate的命令如下: java -jar dgat...
摘要:機翻潤色這個模塊可以讓你在你的應(yīng)用中通過使用以下簡稱認證請求這個文檔做了一個很好的介紹如果你使用版本同時你有一個低于版本的安裝主分支倉庫上的譯者注上的版本使用所以必須是以上如果你使用你需要從安裝這個代碼在分支上安裝用例認證中間件 KOA-JWT (機翻潤色) node>=7.6.0 npm v3.2.2 這個模塊可以讓你在你的KOA應(yīng)用中通過使用JSON WEB TOKEN(以下簡稱J...
摘要:小程序官方流程圖如下,官方地址如果此圖理解不清楚的地方也可參看我的博客本文是對接微信小程序自定義登錄的一個完整例子實現(xiàn),技術(shù)棧為。調(diào)用微信接口獲取和根據(jù)和自定義登陸態(tài)返回自定義登陸態(tài)給小程序端。 小程序官方流程圖如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login....
摘要:的安全性不好,攻擊者可以通過獲取本地進行欺騙或者利用進行攻擊。 好久沒寫博客了,因為最近公司要求我學spring cloud ,早點將以前軟件遷移到新的架構(gòu)上。所以我那個拼命的學吶,總是圖快,很多關(guān)鍵的筆記沒有做好記錄,現(xiàn)在又遺忘了很多關(guān)鍵的技術(shù)點,極其罪惡! 現(xiàn)在想一想,還是踏踏實實的走比較好。這不,今天我冒了個泡,來補一補前面我所學所忘的知識點。 想要解鎖更多新姿勢?請訪問我的博客...
閱讀 2048·2023-04-25 15:11
閱讀 3490·2021-09-23 11:57
閱讀 1379·2021-07-26 23:38
閱讀 1324·2019-08-30 15:54
閱讀 642·2019-08-30 15:53
閱讀 3251·2019-08-26 13:36
閱讀 995·2019-08-26 12:01
閱讀 2870·2019-08-23 16:21