快速参考
私有 Registry 认证配置快速参考卡片
服务端快速配置
生成令牌
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"配置环境变量
REGISTRY_TOKEN=your_generated_token客户端快速配置
配置 components.json
{
"registries": {
"@acme": {
"url": "https://your-domain.com/api/registry/{name}",
"headers": {
"Authorization": "Bearer ${REGISTRY_TOKEN}"
}
}
}
}设置环境变量
REGISTRY_TOKEN=your_token认证方式速查
| 方式 | 请求头/参数 | 示例 | 推荐度 |
|---|---|---|---|
| Bearer Token | Authorization | Bearer YOUR_TOKEN | ⭐⭐⭐⭐⭐ |
| API Key | X-API-Key | YOUR_TOKEN | ⭐⭐⭐⭐ |
| 查询参数 | token | ?token=YOUR_TOKEN | ⭐⭐ (仅测试) |
测试命令速查
成功请求(200)
curl -H "Authorization: Bearer YOUR_TOKEN" \
http://localhost:3000/api/registry/hello-world无认证(401)
curl http://localhost:3000/api/registry/hello-world错误令牌(401)
curl -H "Authorization: Bearer wrong_token" \
http://localhost:3000/api/registry/hello-worldHTTP 状态码
| 状态码 | 含义 | 原因 |
|---|---|---|
| 200 | 成功 | 认证通过,组件返回 |
| 401 | 未授权 | 令牌缺失或无效 |
| 403 | 禁止访问 | 无权限访问该组件 |
| 404 | 未找到 | 组件不存在 |
| 500 | 服务器错误 | 服务器内部错误 |
环境变量
| 变量名 | 必需 | 说明 |
|---|---|---|
REGISTRY_TOKEN | ✅ | Registry 认证令牌 |
JWT_SECRET | ❌ | JWT 密钥(高级) |
DATABASE_URL | ❌ | 数据库连接(高级) |
常用文件路径
project/
├── .env.local # 环境变量
├── env.example # 环境变量示例
├── registry.json # Registry 配置
├── app/api/registry/[name]/route.ts # API 路由
├── content/docs/registry/authentication.mdx # 认证文档
└── scripts/test-auth.sh # 测试脚本故障排查速查
问题:401 Unauthorized
解决方案:
- 检查环境变量:
echo $REGISTRY_TOKEN - 确认令牌格式正确
- 重启开发服务器
- 检查
.env.local文件
问题:403 Forbidden
解决方案:
- 令牌有效但无权限
- 检查 RBAC 配置
- 联系管理员
问题:404 Not Found
解决方案:
- 确认组件名称正确
- 运行
pnpm registry:build - 检查
public/r/目录