Skip to content

新增 IP 封禁规则

1. 接口定位

  • 接口名称: 新增 IP 封禁规则
  • 所属域: admin/forbidden/ip
  • 业务目标: 批量写入 IP 的注册/登录限制规则

2. 请求定义

  • Method: POST
  • Path: /forbidden/ip/add
  • Content-Type: 推荐 application/json
  • operationID: 必填,请通过 Header operationID 传入
  • 鉴权: 需要 Header token,且必须是管理员 token
  • 幂等性: 非幂等;重复添加同一 IP 依赖数据库唯一索引,冲突时会失败

3. 请求参数

Header 参数

字段必填类型说明
operationIDstring链路追踪 ID
tokenstring管理员 token

Body 参数

字段必填类型说明
forbiddensarray<object>要新增的 IP 限制规则列表

forbiddens 元素字段

字段必填类型说明
ipstring目标 IP
limitRegisterbool是否禁止该 IP 注册
limitLoginbool是否禁止该 IP 登录

字段约束

  • 协议校验要求 forbiddens 不能为 null
  • 当前 RPC 层不会额外校验 ip 是否为空,也不会强制要求两个布尔值至少一个为 true;文档按真实实现描述。
  • 数据库对 ip 建有唯一索引,重复添加相同 IP 会由数据库返回冲突错误。

4. 响应结构

通用响应包裹

字段类型说明
errCodeint错误码,0 表示成功
errMsgstring错误简述
errDltstring错误详情
dataobject业务数据

data 字段

  • 本接口成功时返回空对象(无业务字段)。

5. 业务规则

  • 仅管理员可以调用。
  • 每条规则会落库为:ip + limitRegister + limitLogin + createTime
  • 同一次请求中的多条记录使用同一个服务端 createTime
  • 本接口仅写入限制表,不会立即踢下线或清理现有会话。

6. 错误码与失败场景

错误码场景典型报错
1001forbiddensnullforbiddens is empty
-数据库唯一索引冲突由数据库层返回原始错误
-数据库写入失败由数据库层返回原始错误

7. 示例

fetch 请求示例

javascript
fetch("http://localhost:10009/forbidden/ip/add", {
  method: "POST",
  headers: {
    operationID: "550e8400-e29b-41d4-a716-446655440405",
    token: "eyJhbGciOi...",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    forbiddens: [
      {
        ip: "203.0.113.8",
        limitRegister: true,
        limitLogin: true,
      },
      {
        ip: "198.51.100.20",
        limitRegister: false,
        limitLogin: true,
      },
    ],
  }),
})
  .then((res) => res.json())
  .then((data) => console.log(data));

请求示例(JSON)

json
{
  "forbiddens": [
    {
      "ip": "203.0.113.8",
      "limitRegister": true,
      "limitLogin": true
    },
    {
      "ip": "198.51.100.20",
      "limitRegister": false,
      "limitLogin": true
    }
  ]
}

成功响应示例

json
{
  "errCode": 0,
  "errMsg": "",
  "errDlt": "",
  "data": {}
}

失败响应示例

json
{
  "errCode": 1001,
  "errMsg": "ArgsError",
  "errDlt": "forbiddens is empty"
}

8. 时序流程

  1. 中间件校验管理员 token。
  2. 校验 forbiddens 是否为 null
  3. 将请求体转换为数据库模型,并补充当前服务端时间。
  4. 批量写入 IP 限制表。
  5. 返回统一成功响应。

9. 变更记录

  • 2026-03-31: 首版发布,基于 IP 封禁规则批量写入逻辑补全文档。