为 JupyterHub 配置统一身份认证,支持 OAuth2 和 LDAP 两种方式。

OAuth 认证(推荐)

使用 GenericOAuthenticator 实现 Authentik 登录。

values.yaml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
hub:
config:
# 通用认证配置
Authenticator:
admin_users:
- "admin"
allow_all: true # 允许所有认证用户登录
auto_login: true # 自动跳转登录

# OAuth 配置
GenericOAuthenticator:
client_id: "your-client-id"
client_secret: "your-client-secret"
login_service: "OAuth"
# Authentik 端点
authorize_url: "https://authentik.example.com/application/o/authorize/"
token_url: "https://authentik.example.com/application/o/token/"
userdata_url: "https://authentik.example.com/application/o/userinfo/"
# 回调地址(必须在 Authentik 中配置)
oauth_callback_url: "https://jupyter.example.com/hub/oauth_callback"
# Scope 配置
scope:
- "openid"
- "email"
- "profile"
# 用户名字段
username_claim: "sub" # 使用 sub 作为用户名

# JupyterHub 全局配置
JupyterHub:
authenticator_class: "generic-oauth"
admin_access: true # 管理员可以访问所有用户的 Notebook

配置说明

配置项 说明 可选值
allow_all 是否允许所有用户 true / false
auto_login 自动跳转登录 true / false
username_claim 用户名字段 sub / email / preferred_username
oauth_callback_url 回调地址 必须与 Authentik 配置一致

LDAP 认证(备选)

使用 LDAPAuthenticator 实现 LDAP 登录。

values.yaml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
hub:
config:
Authenticator:
admin_users:
- "admin"
allow_all: true

# LDAP 配置
LDAPAuthenticator:
server_address: "ldap.example.com"
use_ssl: true
# 绑定模板
bind_dn_template:
- "uid={username},ou=foo,ou=People,dc=example,dc=com"
# 组限制(可选)
allowed_groups: []
# 安全配置
escape_userdn: true
lookup_dn: false

JupyterHub:
authenticator_class: "ldapauthenticator.LDAPAuthenticator"
admin_access: true

LDAP 配置说明

  • bind_dn_template: 用户 DN 模板,{username} 会被替换为实际用户名
  • allowed_groups: 限制可登录的 LDAP 组
  • escape_userdn: 转义用户名中的特殊字符,防止 LDAP 注入

并发配置

控制同时启动 Notebook 的数量:

1
2
3
4
hub:
concurrentSpawnLimit: 64 # 同时启动的最大数量
activeServerLimit: null # 活跃 Server 的最大数量
allowNamedServers: false # 是否允许命名 Server

参考文档

注意事项

  • 回调地址 必须在 Authentik 的应用配置中添加
  • allow_all: true 会允许所有认证用户登录,生产环境建议配合 allowed_groups 限制
  • username_claim 选择 sub 可以避免用户名冲突
  • OAuth 方式比 LDAP 更灵活,支持多种 SSO 平台