8
n8n 中文网amn8n.com

具有 Bearer Token 认证和字段验证的安全 API 端点

高级

这是一个Engineering领域的自动化工作流,包含 16 个节点。主要使用 If, Set, Code, Webhook, RespondToWebhook 等节点。 具有 Bearer Token 认证和字段验证的安全 API 端点

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)

分类

工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "dfb8aefc80b77b230bd90d6a6e5210eb7a28e6e1d2a8b1d27d54942b54eb9e7a",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "4f42007b-3813-410f-a608-5af89459b14f",
      "name": "检查授权头",
      "type": "n8n-nodes-base.if",
      "position": [
        -20,
        20
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $('Webhook').item.json.headers.authorization }}",
              "value2": "=Bearer {{ $json.config.bearerToken }}"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "86d6157e-593d-4370-a480-1a9417300555",
      "name": "401 未授权",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        340,
        280
      ],
      "parameters": {
        "options": {
          "responseCode": 401
        },
        "respondWith": "json",
        "responseBody": "{\n  \"code\": 401,\n  \"message\": \"Unauthorized: Missing or invalid authorization token.\",\n  \"hint\": \"Ensure the request includes a valid 'Authorization' header (e.g., 'Bearer YOUR_SECRET_TOKEN').\"\n}"
      },
      "typeVersion": 1
    },
    {
      "id": "0831093a-adef-41dc-8ac0-2e1998fc22ad",
      "name": "200 成功",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1140,
        20
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "b4f42651-c7f6-43a3-a695-7d5197b45642",
      "name": "配置",
      "type": "n8n-nodes-base.set",
      "position": [
        -300,
        20
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "4c35898d-5a70-41bc-9fb6-9d63bbbee222",
              "name": "config.bearerToken",
              "type": "string",
              "value": "123"
            },
            {
              "id": "822739a6-15da-48df-8f92-c4b1adce5fef",
              "name": "config.requiredFields.message",
              "type": "string",
              "value": "true"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "f1539109-8585-4cf2-9b9b-f3012544ac6c",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -580,
        20
      ],
      "webhookId": "2c5b9b70-1b08-44b1-a007-dc3d9f7e70db",
      "parameters": {
        "path": "secure-webhook",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 1
    },
    {
      "id": "bcf1183c-9a3d-41eb-89f7-1666d3a6c5fc",
      "name": "包含必需字段吗?",
      "type": "n8n-nodes-base.code",
      "position": [
        220,
        20
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "if(! $json.config.requiredFields) {\n  return { json: { valid: true } };\n}\n\nconst body = $('Webhook').first().json.body;\n\nlet requiredFields = $json.config.requiredFields;\n\nfor (let [key, value] of Object.entries(requiredFields)) {\n  console.log(`${key}: ${value}`);\n  if (!(key in body)) {\n    return { json: { valid: false } };\n  }\n}\n\nreturn { json: { valid: true } };"
      },
      "typeVersion": 2
    },
    {
      "id": "81b125f1-faa0-4998-8624-431746052a84",
      "name": "检查有效请求",
      "type": "n8n-nodes-base.if",
      "position": [
        440,
        20
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "8c7fe174-f284-4e41-b851-8939f0c2d19f",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.valid }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "906c671d-e2a6-4a9e-b7df-d7b9142ffeb4",
      "name": "400 错误请求",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        780,
        280
      ],
      "parameters": {
        "options": {
          "responseCode": 401
        },
        "respondWith": "json",
        "responseBody": "{\n  \"code\": 400,\n  \"message\": \"Bad Request: Missing required fields\",\n  \"hint\": \"Make sure all required fields are included in the request body.\"\n}"
      },
      "typeVersion": 1
    },
    {
      "id": "ce657170-34e4-4b40-ba22-bb4638fa98c6",
      "name": "创建响应",
      "type": "n8n-nodes-base.set",
      "position": [
        920,
        20
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "c6258b81-6f40-4dd5-8a60-89e2b0322490",
              "name": "message",
              "type": "string",
              "value": "Success! Workflow completed."
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "0a6b9f12-9b60-458e-85de-014a66063e50",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -440,
        -280
      ],
      "parameters": {
        "color": 6,
        "width": 360,
        "height": 460,
        "content": "### 🛠️ 配置节点设置"
      },
      "typeVersion": 1
    },
    {
      "id": "bba24ba5-3c8d-40f7-99e0-44115b1025e0",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -440,
        200
      ],
      "parameters": {
        "color": 3,
        "width": 1740,
        "height": 240,
        "content": "### 🚫 错误处理节点"
      },
      "typeVersion": 1
    },
    {
      "id": "f451c9be-4cfb-4628-8aa7-66b66ad86bab",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        840,
        -280
      ],
      "parameters": {
        "color": 4,
        "width": 460,
        "height": 460,
        "content": "### ✅ 设置与 200 响应节点"
      },
      "typeVersion": 1
    },
    {
      "id": "8d4e8406-c3fe-4e8a-bfa8-18407fe5e67a",
      "name": "在此处添加工作流节点",
      "type": "n8n-nodes-base.noOp",
      "position": [
        680,
        20
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "f3f461a6-dc48-42cd-ac75-d045795006d0",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        -280
      ],
      "parameters": {
        "color": 7,
        "width": 440,
        "height": 460,
        "content": "### 🔍 必需字段验证器"
      },
      "typeVersion": 1
    },
    {
      "id": "2766dae8-8def-462f-a53c-0f51606eea0a",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1220,
        -340
      ],
      "parameters": {
        "color": 5,
        "width": 760,
        "height": 780,
        "content": "## 🔐 安全 Webhook – 摘要"
      },
      "typeVersion": 1
    },
    {
      "id": "70c8f060-587a-4524-ab32-7362cc0c4cf9",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1220,
        -600
      ],
      "parameters": {
        "color": 6,
        "width": 760,
        "height": 240,
        "content": "## 支持我的工作! ❤️"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "200 OK": {
      "main": [
        []
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Configuration": {
      "main": [
        [
          {
            "node": "Check Authorization Header",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Response": {
      "main": [
        [
          {
            "node": "200 OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Valid Request": {
      "main": [
        [
          {
            "node": "Add workflow nodes here",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "400 Bad Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has required fields?": {
      "main": [
        [
          {
            "node": "Check Valid Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add workflow nodes here": {
      "main": [
        [
          {
            "node": "Create Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Authorization Header": {
      "main": [
        [
          {
            "node": "Has required fields?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "401 Unauthorized",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 工程

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量16
分类1
节点类型7
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Audun

Audun

@xqus

I’m an automagician who loves turning complex systems into simple, powerful workflows. Here to help you automate smarter and work easier.

外部链接
在 n8n.io 查看

分享此工作流