Skip to content

添加多租户支持和API Key认证 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

wizd
Copy link

@wizd wizd commented May 5, 2025

添加多租户支持和API Key认证

概述

本PR为RestServerTransport类添加了两个重要的新特性:多租户支持和API Key认证。这些功能增强了SDK的安全性和多租户场景下的应用能力。

变更详情

多租户支持

  • RestServerTransportOptions接口添加了supportTenantId选项
  • 实现了根据URL路径参数解析租户ID的功能:/endpoint/:tenantId
  • 自动将租户ID作为_tenantId参数注入到每个请求的params对象中
  • 支持在请求处理程序中获取和使用租户ID进行权限控制和数据隔离

API Key认证

  • RestServerTransportOptions接口添加了apiKeyapiKeyHeaderName选项
  • 实现了从HTTP请求头提取和验证API Key的功能
  • 支持自定义API Key的请求头名称(默认为X-API-Key
  • 对无效的API Key请求返回标准的401 Unauthorized错误

使用示例

多租户支持

const transport = new RestServerTransport({ 
  endpoint: '/api', 
  port: 9593, 
  supportTenantId: true 
});

// 客户端可通过 /api/tenant1, /api/tenant2 等不同路径访问

// 在请求处理程序中获取租户ID
server.setRequestHandler(SomeSchema, async (request) => {
  const tenantId = request.params._tenantId;
  // 使用tenantId进行数据隔离或权限控制
});

API Key认证

const transport = new RestServerTransport({ 
  endpoint: '/secure', 
  port: 9593, 
  apiKey: 'your-secret-api-key'
});

// 客户端需在请求头中包含 X-API-Key: your-secret-api-key

测试方法

  1. 多租户测试:使用不同的租户ID路径发送请求,验证租户ID被正确传递
  2. API Key测试
    • 使用正确的API Key发送请求,验证请求成功处理
    • 使用错误的API Key发送请求,验证返回401错误
    • 不设置API Key选项时,验证所有请求都被允许

文档更新

  • 更新了README.md,添加了多租户支持和API Key认证的详细使用说明
  • 添加了代码示例,展示如何配置和使用这些新特性
  • 示例中包括了如何在客户端和服务端正确使用这些功能

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant