https://github.com/Kong/kong/tree/master/kong/plugins/request-termination
handler.lua
local kong = kong
local DEFAULT_RESPONSE = {
[401] = "Unauthorized",
[404] = "Not found",
[405] = "Method not allowed",
[500] = "An unexpected error occurred",
[502] = "Bad Gateway",
[503] = "Service unavailable",
}
local RequestTerminationHandler = {}
RequestTerminationHandler.PRIORITY = 2
RequestTerminationHandler.VERSION = "2.0.1"
function RequestTerminationHandler:access(conf)
local status = conf.status_code
local content = conf.body
if content then
local headers = {
["Content-Type"] = conf.content_type
}
return kong.response.exit(status, content, headers)
end
local message = conf.message or DEFAULT_RESPONSE[status]
return kong.response.exit(status, message and { message = message } or nil)
end
return RequestTerminationHandler
schema.lua
local typedefs = require "kong.db.schema.typedefs"
local is_present = function(v)
return type(v) == "string" and #v > 0
end
return {
name = "request-termination",
fields = {
{ protocols = typedefs.protocols_http },
{ config = {
type = "record",
fields = {
{ status_code = {
type = "integer",
default = 503,
between = { 100, 599 },
}, },
{ message = { type = "string" }, },
{ content_type = { type = "string" }, },
{ body = { type = "string" }, },
},
custom_validator = function(config)
if is_present(config.message)
and(is_present(config.content_type)
or is_present(config.body)) then
return nil, "message cannot be used with content_type or body"
end
if is_present(config.content_type)
and not is_present(config.body) then
return nil, "content_type requires a body"
end
return true
end,
},
},
},
}
参数 | 默认值 | 描述 |
---|---|---|
name |
要使用的插件的名称,在本例中为ip-restriction |
|
service_id |
此插件将定位的 Service 的ID。 | |
route_id |
此插件将定位的 Route 的ID。 | |
enabled |
true |
是否将应用此插件。 |
consumer_id |
此插件将定位的Consumer的id | |
config.status_code optional |
要发送的响应代码。 | |
config.message optional |
要使用默认响应生成器发送的消息。 | |
config.body optional |
要发送的原始响应body,这与config.message 字段互斥。 |
|
config.content_type optional |
application/json; charset=utf-8 |
使用config.body 配置的原始响应的Content type。 |
使用后,将通过发送配置的响应立即终止每个请求(在 Service, Route, Consumer, 或全局的已配置插件范围内)。
使用示例
- 暂时禁用 Service(例如,它正在维护中)。
- 暂时禁用 Route(例如,服务的其余部分已启动并正在运行,但必须禁用特定端点)。
- 暂时禁用 Consumer(例如过度消费)。
- 在逻辑
OR
设置中使用多个auth插件阻止匿名访问。