配置基于apisix 3.9.1版本
场景1:Get Api增加 uri 参数,配置示例
#curl "http://192.168.11.159:8080/v2/open-api/muti-icp-info?dm=52pojie.cn----->apisix补齐这个参数(&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后插入请求头,其他参数不改变,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内容,body增加zone_key_id = "1111111111111111ae"参数
#注意事项,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
}