配置基于apisix 3.9.1版本
場景1:Get Api增加 uri 參數(shù),配置示例
#curl "http://192.168.11.159:8080/v2/open-api/muti-icp-info?dm=52pojie.cn----->apisix補齊這個參數(shù)(&uc=w&key=h)"
#應用場景,uc和key分別表示賬號和密碼,如果源站安全措施較弱,可使用apisix代理加固,避免暴露源站賬密
{
"uri": "/v2/*",
"name": "xuanji",
"methods": [
"GET",
"POST",
"HEAD",
"OPTIONS"
],
"plugins": {
"serverless-pre-function": {
"functions": [
"return function(conf, ctx) local args = ngx.req.get_uri_args(); args.uc = 'w'; args.key = 'h'; ngx.req.set_uri_args(args) end"
],
"phase": "rewrite"
}
},
"upstream_id": "583645207988274031",
"status": 1
}
場景2:GET Api 實時計算token后插入請求頭,其他參數(shù)不改變,ngx.req.set_header("Token", token); ngx.req.set_header("Timespan", timespan);
{
"uri": "/EnfoVerify/*",
"name": "cc",
"methods": [
"GET",
"POST"
],
"plugins": {
"hmac-auth": {},
"proxy-rewrite": {
"host": "api.qica.com",
"method": "GET",
"scheme": "https",
"uri": "/EnfoVerify/GetInfo?key=44"
},
"serverless-pre-function": {
"disable": false,
"functions": [
"return function(conf, ctx) local core = require(\"apisix.core\"); local appKey = \"44\"; local secretKey = \"64251833\"; local timespan = tostring(ngx.time()); local token_src = appKey .. timespan .. secretKey; local md5 = require(\"resty.md5\"); local digest = md5.new(); digest:update(token_src); local function to_hex(str) return (string.gsub(str, \".\", function(c) return string.format(\"%02x\", string.byte(c)) end)) end; local token = string.upper(to_hex(digest:final())); ngx.req.set_header(\"Token\", token); ngx.req.set_header(\"Timespan\", timespan); core.log.warn(\"[GET] Added headers: Token=\", token, \", Timespan=\", timespan) end"
],
"phase": "rewrite"
}
},
"upstream_id": "585782187589108591",
"status": 1
}
場景3:Post Api修改body內(nèi)容,body增加zone_key_id = "1111111111111111ae"參數(shù)
#注意事項,apisix會自動增加client ip到X-Forwarded-Host,如果源站校驗,需要在proxy-rewrite-->headers-->X-Forwarded-Host值,類似的請求頭還有其他的,可以按類似方法處理
{
"uri": "/zero/api/*",
"name": "0one",
"plugins": {
"hmac-auth": {},
"proxy-rewrite": {
"headers": {
"set": {
"Host": "0.one",
"X-Forwarded-Host": "0.one"
}
},
"host": "0.one",
"method": "POST",
"scheme": "https",
"uri": "/api/data/"
},
"serverless-pre-function": {
"disable": false,
"functions": [
"return function(conf, ctx) local core = require(\"apisix.core\"); local headers = ngx.req.get_headers(); local function safe_get(h, key); return h[key] or h[string.lower(key)]; end ;local ct = headers[\"Content-Type\"]; if not ct or not string.find(ct, \"application/json\", 1, true) then core.log.info(\"skip non-json request, Content-Type: \", ct or \"nil\"); return end; local body = core.request.get_body(); if not body or body == \"\" then body = \"{}\" end; local json, err = core.json.decode(body); if not json then core.log.warn(\"invalid json, using empty object. error: \", err); json = {} end; json.zone_key_id = \"1111111111111111ae\"; local new_body, encode_err = core.json.encode(json); if not new_body then core.log.error(\"failed to encode json: \", encode_err); return end; ngx.req.set_body_data(new_body); ngx.req.clear_header(\"Content-Length\");ngx.req.set_header(\"Content-Length\", string.len(new_body));end"
],
"phase": "rewrite"
}
},
"upstream_id": "585782187589108591",
"status": 1
}