问题描述:apisix接收到的post请求中请求参数中的中文是unicode格式,导致apisix无法对指定字符进行拦截。
解决:apisix中利用Lua脚本对unicode编码字符进行utf-8转换,再进行拦截。
functions中配置上图函数。
"functions": [
"return function(conf, ctx) local core = require(\"apisix.core\") local payload, err = core.request.get_body() if not payload then local uri_args, err = core.request.get_uri_args(ctx) if uri_args then payload = core.json.encode(uri_args, true) end end if not ngx.re.match(payload, '[\\u{0080}-\\u{FFFF}]') then payload = (payload:gsub('\\\\u(%x%x%x%x)', function(c) local n = tonumber(c, 16) if n < 0x80 then return string.char(n) elseif n < 0x800 then return string.char(0xC0 + math.floor(n / 64), 0x80 + n % 64) else return string.char(0xE0 + math.floor(n / 4096), 0x80 + math.floor(n / 64) % 64, 0x80 + n % 64) end end)) end local m = ngx.re.match(payload, \"长城\", \"jo\") if m then ngx.exit(401) end end"
]
问题解决!