用户认证与权限
RuleGo-Server 提供灵活的用户认证与权限体系,支持 JWT 令牌和 API Key 两种认证方式,并可通过接口自定义认证器和授权器。
# 用户管理
在 config.conf 的 [users] 段配置用户:
[users]
# 格式:username = password[,apiKey]
# apiKey 可选
admin = admin,ak-2af255ea5618467d914c67a8beeca31d
user01 = user01
user02 = user02,ak-another-key
1
2
3
4
5
6
2
3
4
5
6
每个用户拥有独立的工作空间,规则链、组件、配置等数据按用户隔离。
# 认证方式
# 匿名模式(默认)
当 require_auth = false 且请求未携带认证信息时,以 default_username(默认 admin)身份访问:
require_auth = false
default_username = admin
1
2
2
# JWT 认证
开启认证:
require_auth = true
jwt_secret_key = your-secret-key
jwt_expire_time = 43200000
jwt_issuer = rulego.cc
1
2
3
4
2
3
4
# 登录获取令牌
POST /api/v1/login
Content-Type: application/json
{
"username": "admin",
"password": "admin"
}
1
2
3
4
5
6
7
2
3
4
5
6
7
响应:
{
"token": "eyJhbGciOiJIUzI1NiIs...",
"expiresAt": 1719360000
}
1
2
3
4
2
3
4
expiresAt为 Unix 时间戳(秒)。登录接口有速率限制:同一 IP 每分钟最多 10 次,超限返回 429。
# 使用令牌
在后续请求中通过 Authorization 头携带令牌:
GET /api/v1/rules
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
1
2
2
# API Key 认证
为用户配置 apiKey 后,可直接使用 API Key 代替 JWT:
方式一:Authorization 头
GET /api/v1/rules
Authorization: Bearer ak-2af255ea5618467d914c67a8beeca31d
1
2
2
方式二:X-API-Key 头
GET /api/v1/rules
X-API-Key: ak-2af255ea5618467d914c67a8beeca31d
1
2
2
API Key 常用于 MCP 客户端接入、第三方系统集成等场景,无需登录流程。
# 权限体系
# 权限动作
| 资源 | 动作 | 说明 |
|---|---|---|
rule | read / write / delete / execute / operate | 规则链管理 |
component | read / write / delete | 组件管理 |
config | read / write | 系统配置 |
log | read / delete | 运行日志 |
locale | read / write | 国际化 |
marketplace | read | 组件市场 |
# 默认授权器
默认情况下,DefaultAuthorizer 允许所有操作,不做权限限制。
# 自定义认证器/授权器
RuleGo-Server 的认证和授权是可替换的,通过服务容器注入自定义实现:
| 服务键 | 接口 | 说明 |
|---|---|---|
module.user.authenticator | Authenticator | 自定义认证逻辑(OAuth2、LDAP 等) |
module.user.authorizer | Authorizer | 自定义授权逻辑(RBAC、ABAC 等) |
二次开发参见 二次开发。
在 GitHub 上编辑此页 (opens new window)
上次更新: 2026/05/28, 01:50:04