apisix插件之修改返回body

博客给出了修改请求返回body的插件代码,聚焦于信息技术中请求处理相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、修改请求返回body的插件,直接上代码:

local core = require("apisix.core")
local ngx = ngx
local string = string
--插件配置json语法
local schema = {
    properties = {
        before_body = {
            description = "body before the filter phase.",
            type = "string"
        },
        body = {
            description = "body to replace upstream response.",
            type = "string"
        },
        after_body = {
            description = "body after the modification of filter phase.",
            type = "string"
        }
    },
    anyOf = {
        {required = {"before_body"}},
        {required = {"body"}},
        {required = {"after_body"}}
    },
    minProperties = 1
}
--插件名称
local plugin_name = "eag"

local _M = {
    version = 0.1,
    priority = 20004, --插件优先级
    name = plugin_name,
    schema = schema,
}
--插件配置检验
function _M.check_schema(conf)
    return core.schema.check(schema, conf)
end
--返回值body修改同时必须修改ngx.header.content_length
function _M.body_filter(conf, ctx)
    local request_uri = ngx.var.uri  

    if conf.after_body then
        ngx.arg[1] = string.upper(conf.body)
    end

    ngx.arg[2] = true
end
--修改ngx.header.content_length
function _M.header_filter(conf, ctx)
    if conf.body then
        ngx.header.content_length = #conf.body
        -- in case of upstream content is compressed content
        ngx.header.content_encoding = nil
    end
end

 

### APISix 中创建自定义插件实现敏感数据脱敏 在 APISix 中,通过编写 Lua 插件可以轻松实现对请求或响应中的敏感数据进行脱敏处理。这不仅有助于保护用户隐私,也符合许多行业的合规要求。 #### 创建自定义插件结构 为了创建一个新的插件,在 `apisix/plugins` 目录下新建文件夹并命名为 `data_masking`。该目录应包含至少两个主要文件: - `init.lua`: 定义插件的核心逻辑。 - `_schema.json`: 描述配置项及其验证规则[^1]。 #### 编写核心逻辑 (init.lua) 下面是一个简单的例子来展示如何构建一个用于遮蔽信用卡号的插件: ```lua local core = require("apisix.core") local plugin_name = "data-mask" local schema = { type = "object", properties = { mask_pattern = {type = "string", default = "(\\d{4})\\d+(?=\\d{4}$)"} } } local _M = { version = 0.1, priority = 987, -- 设置优先级以控制执行顺序 name = plugin_name, schema = schema, } function _M.check_schema(conf) local ok, err = core.schema.check(schema, conf) if not ok then return false, err end return true end function _M.access(conf, ctx) local headers = ngx.req.get_headers() local body_data = ngx.var.request_body if body_data and string.match(body_data, "%d%d%d%d") then local masked_body = string.gsub(body_data, conf.mask_pattern, "%1****%2") ngx.req.set_body_data(masked_body) end for k, v in pairs(headers) do if type(v) == 'string' and string.match(v, "%d%d%d%d") then local new_v = string.gsub(v, conf.mask_pattern, "%1****%2") ngx.header[k] = new_v end end end return _M ``` 此代码片段展示了如何拦截 HTTP 请求体以及头部信息,并应用正则表达式模式匹配特定格式的数据(如信用卡号码),将其替换为部分隐藏的形式[^2]。 #### 配置与启用插件 完成上述步骤之后,可以通过 API 或者命令行工具加载新编写的插件到指定路由上: ```bash curl http://127.0.0.1:9080/apisix/admin/routes/1 \ -H 'X-API-KEY: your_api_key_here' -X PUT -d ' { "plugins": { "data-mask": {} }, "upstream": { "nodes": {"httpbin.org:80": 1}, "type": "roundrobin" }, "uri": "/get" }' ``` 这段 JSON 配置指定了当访问 `/get` URI 时会触发 `data-mask` 插件的工作流程[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值