apisix网关ip-restriction插件使用说明

news/2025/2/10 15:56:18/

ip-restriction插件可以在网关层进行客户端请求ip拦截。

当然了,一般不推荐使用该方法,专业的事专业工具做。建议有条件,还是上防火墙或者waf来做。

官方文档:ip-restriction | Apache APISIX® -- Cloud-Native API Gateway

whitelist:白名单,配置允许访问的ip。

blacklist:黑名单,配置禁止访问的ip。

估计有朋友要问了,我上面那个external_auth的配置节干什么用的。

这个由于业务需要,我们做了一个动态校验请求。就是拦截后会请求这个链接,软如参数,然后如果返回200,就自动放通,如果返回其他状态,就拦截。

这个是自己写脚本实现的。代码如下,其实就是改了ip-restriction的脚本,增加了一个参数。

---
apiVersion: v1
data:init.lua: |---- Licensed to the Apache Software Foundation (ASF) under one or more-- contributor license agreements.  See the NOTICE file distributed with-- this work for additional information regarding copyright ownership.-- The ASF licenses this file to You under the Apache License, Version 2.0-- (the "License"); you may not use this file except in compliance with-- the License.  You may obtain a copy of the License at----     http://www.apache.org/licenses/LICENSE-2.0---- Unless required by applicable law or agreed to in writing, software-- distributed under the License is distributed on an "AS IS" BASIS,-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-- See the License for the specific language governing permissions and-- limitations under the License.--local ipairs    = ipairslocal core      = require("apisix.core")local http   = require("resty.http")local lrucache  = core.lrucache.new({ttl = 300, count = 512})local schema = {type = "object",properties = {message = {type = "string",minLength = 1,maxLength = 1024,default = "Your IP address is not allowed"},whitelist = {type = "array",items = {anyOf = core.schema.ip_def},minItems = 1},blacklist = {type = "array",items = {anyOf = core.schema.ip_def},minItems = 1},external_auth = {type = "object",properties = {uri = {type = "string"},timeout = {type = "integer",minimum = 1,maximum = 60000,default = 3000,description = "timeout in milliseconds",},method = {type = "string",default = "GET",enum = {"GET", "POST"},description = "the method for client to request the auth service"},headers = {type = "object"},ip_param_key = {type = "string", default = "ip"},allow_degradation = {type = "boolean", default = true},rejected_code = {type = "integer", minimum = 200, maximum = 599, default = 403},rejected_msg = {type = "string", minLength = 1}}}},anyOf = {{required = {"whitelist"}},{required = {"blacklist"}},{required = {"external_auth"}},}}local plugin_name = "ip-restriction"local _M = {version = 0.1,priority = 3000,name = plugin_name,schema = schema,}function _M.check_schema(conf)local ok, err = core.schema.check(schema, conf)if not ok thenreturn false, errend-- we still need this as it is too complex to filter out all invalid IPv6 via regexif conf.whitelist thenfor _, cidr in ipairs(conf.whitelist) doif not core.ip.validate_cidr_or_ip(cidr) thenreturn false, "invalid ip address: " .. cidrendendendif conf.blacklist thenfor _, cidr in ipairs(conf.blacklist) doif not core.ip.validate_cidr_or_ip(cidr) thenreturn false, "invalid ip address: " .. cidrendendendreturn trueendfunction _M.restrict(conf, ctx)local remote_addr = ctx.var.remote_addrlocal block = falseif conf.blacklist thenlocal matcher = lrucache(conf.blacklist, nil,core.ip.create_ip_matcher, conf.blacklist)if matcher thenblock = matcher:match(remote_addr)endendif block then-- 黑名单中的 ip 直接拒绝return 403, { message = conf.message }endlocal in_white = falseif conf.whitelist thenlocal matcher = lrucache(conf.whitelist, nil,core.ip.create_ip_matcher, conf.whitelist)if matcher thenin_white = matcher:match(remote_addr)endendif in_white then-- 白名单中的 ip 直接放行returnendif conf.external_auth thenlocal external_auth = conf.external_authlocal params = {method = external_auth.request_method}local httpc = http.new()httpc:set_timeout(external_auth.timeout)local uri = external_auth.ip_param_key .. '=' .. remote_addrif string.find(external_auth.uri, "?") thenuri = external_auth.uri .. "&" .. urielseuri = external_auth.uri .. "?" .. uriendlocal res, err = httpc:request_uri(uri, params)-- 校验 ip 的服务不可用的时候if not res thencore.log.error("failed to auth ip, err: ", err)if conf.external_auth.allow_degradation then-- 允许放行returnelsereturn external_auth.rejected_code, { message = conf.message }endend-- 返回值为 2xx 的时候表示校验通过if res.status >= 300 thenreturn external_auth.rejected_code, { message = conf.message }endendendreturn _M
kind: ConfigMap
metadata:name: ip-restrictionnamespace: apisix-szxc-qxz2v397g6resourceVersion: '224926381'

核心部分:

然后请求的接口,就可以自己编写了,相对更灵活。

对了,上面贴的是k8s的yaml,这是一个comfigmap,注入到apisix容器中,替换了原文件。

其实apisix的组件都是lua脚本实现的,很灵活,都可以根据需要自行重写。


http://www.ppmy.cn/news/1570903.html

相关文章

【手写公式识别】MEMix: Improving HMER with Diverse Formula Structure Augmentation 论文阅读

发表于:ICME 2024 原文链接:https://ieeexplore.ieee.org/document/10687521 源码:无 Abstract 手写数学表达式识别(HMER)旨在将数学表达式(MEs)的图像转换为相应的LaTeX序列。然而&#xff0…

SpringBoot 接口内容加密方案(RSA+AES+HMAC校验)认知

写在前面 工作中遇到,简单整理博文内容涉及 Web接口内容 类似 https 的加密和防篡改校验以及具体Java Springboot 项目中如何编码。理解不足小伙伴帮忙指正 😃,生活加油 99%的焦虑都来自于虚度时间和没有好好做事,所以唯一的解决办法就是行动…

Intellij IDEA如何查看当前文件的类

快捷键:CtrlF12,我个人感觉记快捷键很麻烦,知道具体的位置更简单,如果忘了快捷键(KeyMap)看一下就记起来了,不需要再Google or Baidu or GPT啥的,位置:Navigate > Fi…

html为<td>添加标注文本

样式说明: /*为td添加相对定位点*/ .td_text {position: relative; }/*为p添加绝对坐标(相对于父元素中的定位点)*/ .td_text p {position: absolute;top: 80%;font-size: 8px; }参考资料:

嵌入式硬件篇---OpenMV的硬件流和软件流

文章目录 前言一、硬件流控制(Hardware Flow Control)1. 基本原理RTSCTS 2. OpenMV中的实现• 硬件要求• 代码配置• 工作流程 二、软件流控制(Software Flow Control)1. 基本原理XONXOFF 2. OpenMV中的实现• 代码配置• 工作流…

Angular-hello world

环境搭建- 安装angular-cli 用npm安装typescript和typings npm install -gtypescript typings 安装angular-cl npm install -gangular-clilatest ngnew ProjectName //ProjectName为你的项目名,用此命令建立基本文件,然后强制终止 cd ProjectName cnpm …

【嵌入式 Linux 音视频+ AI 实战项目】瑞芯微 Rockchip 系列 RK3588-基于深度学习的人脸门禁+ IPC 智能安防监控系统

前言 本文主要介绍我最近开发的一个个人实战项目,“基于深度学习的人脸门禁 IPC 智能安防监控系统”,全程满帧流畅运行。这个项目我目前全网搜了一圈,还没发现有相关类型的开源项目。这个项目只要稍微改进下,就可以变成市面上目前…

苍穹外卖 项目记录 day11 Spring Task订单定时处理-来单提醒-客户催单

文章目录 Spring Taskcron表达式Spring Task使用步骤订单状态定时处理WebSocketWebSockt入门示例来单提醒客户催单 Spring Task Spring Task 是Spring框架提供的任务调度工具,可以按照约定的时间自动执行某个代码逻辑。 应用场景: 1). 信用卡每月还款…