跳转到主要内容

LDAP 身份认证

BookStack 可配置为允许基于 LDAP 的用户登录。启用 LDAP 登录后,将无法再使用标准的用户名/密码方式登录,同时新用户注册功能会被禁用。BookStack 仅使用 LDAP 服务器获取用户信息和执行身份认证。LDAP 服务器上的数据无法通过 BookStack 编辑。

此处可查看配置 LDAP 的视频指南。

身份认证配置

LDAP 用户首次登录 BookStack 时,系统会为其创建 BookStack 用户资料,并分配应用程序设置中“注册后的默认用户角色”选项所指定的默认角色。

要配置基于 LDAP 的身份认证,请在 .env 文件中添加或修改以下变量:

# General auth
AUTH_METHOD=ldap

# The LDAP host, Adding a port is optional
LDAP_SERVER=example.com:389
# If using LDAP over SSL you should also define the protocol:
# LDAP_SERVER=ldaps://example.com:636

# The base DN from where users will be searched within
LDAP_BASE_DN="ou=People,dc=example,dc=com"

# The full DN and password of the user used to search the server
# Can both be left as 'false' (without quotes) to bind anonymously
LDAP_DN="cn=serviceaccount,ou=People,dc=example,dc=org"
LDAP_PASS="my#super#secret#password543"

# A filter to use when searching for users
# The user-provided user-name used to replace any occurrences of '{user}'.
# (Also supported via '${user}' for backwards compatibility.
LDAP_USER_FILTER=(&(uid={user}))

# Set the LDAP version to use when connecting to the server
# Should be set to 3 in most cases.
LDAP_VERSION=3

# Set the property to use as a unique identifier for this user.
# Stored and used to match LDAP users with existing BookStack users.
# Prefixing the value with 'BIN;' will assume the LDAP service provides the attribute value as
# binary data and BookStack will convert the value to a hexidecimal representation.
# Defaults to 'uid'.
LDAP_ID_ATTRIBUTE=uid

# Set the default 'email' attribute. Defaults to 'mail'
LDAP_EMAIL_ATTRIBUTE=mail

# Set the property to use for a user's display name. Defaults to 'cn'
LDAP_DISPLAY_NAME_ATTRIBUTE=cn

# Set the attribute to use for the user's avatar image.
# Must provide JPEG binary image data.
# Will be used upon login or registration when the user doesn't
# already have an avatar image set.
# Remove this option or set to 'null' to disable LDAP avatar import.
LDAP_THUMBNAIL_ATTRIBUTE=jpegphoto

# Force TLS to be used for LDAP communication.
# Use this if you can but your LDAP support will need to support it and
# you may need to import your certificate to the BookStack host machine.
# Defaults to 'false'.
LDAP_START_TLS=false

# Certificate Authority Certificates to use for LDAP TLS connections.
# Generally not needed if using a publicly trusted certificates.
# Value should be a path to a specific CA certificate in pem format,
# or a directory path where certificates are named via hashes, as
# can be managed by a utility like "c_rehash".
# Defaults to 'false'.
#LDAP_TLS_CA_CERT=/etc/ldap/certs

# If you need to allow untrusted LDAPS certificates, add the below and uncomment (remove the #)
# Only set this option if debugging or you're absolutely sure it's required for your setup.
# If using php-fpm, you may want to restart it after changing this option to avoid instability.
#LDAP_TLS_INSECURE=true

# If you need to debug the details coming from your LDAP server, add the below and uncomment (remove the #)
# Only set this option if debugging since it will block logins and potentially show private details.
#LDAP_DUMP_USER_DETAILS=true

系统中还需安装 php-ldap 扩展。配置 LDAP 时,可将 APP_DEBUG 变量设为 true,以显示错误信息。请注意,调试模式可能会向访问者暴露敏感信息,因此配置期间你可能需要限制访问权限,并在 LDAP 正常运行后记得将其改回原值。

BookStack 中的用户通过“uid”与 LDAP 用户关联。如果 LDAP 用户的 uid 发生变化,管理员可在 BookStack 中通过修改用户资料页上的“外部身份认证 ID”字段进行更新。

将 AUTH_METHOD 改为 ldap 后,你可能会发现无法使用初始的 Admin 账户登录。此时可将 AUTH_METHOD 设为 standard,使用管理员账户登录,然后将其改回 ldap。随后编辑你的用户资料,在“外部身份认证 ID”字段中填入你的 LDAP uid。此后即可使用该 ID 登录。

Active Directory

BookStack 支持通过 LDAP 使用 Active Directory。你可能需要设置以下参数以配合 AD 使用。请注意,用户筛选器可能需要根据你的具体配置以及系统中管理用户的方式进行调整。你仍需遵循上述配置说明。

LDAP_USER_FILTER=(&(sAMAccountName={user}))
LDAP_VERSION=3
LDAP_ID_ATTRIBUTE=BIN;objectGUID
# Change the below to true if your AD server supports TLS and if your
# BookStack host system will accept the AD provided certificate.
LDAP_START_TLS=false
LDAP_THUMBNAIL_ATTRIBUTE=thumbnailPhoto

LDAP 组同步

BookStack 支持将 LDAP 用户组与 BookStack 角色进行同步。默认情况下,系统会将 LDAP 组名与 BookStack 角色显示名称进行匹配,匹配时忽略大小写。 启用 LDAP 身份认证后,编辑角色时可以看到“外部身份认证 ID”字段,可通过该字段覆盖默认匹配规则。该字段可填写账户或组的通用名称(CN)。填写后,系统将使用该字段中的 CN,并忽略角色名称。多个 CN 之间可用逗号分隔。如果需要映射到包含字面逗号字符的 CN,可用反斜杠转义逗号(\,)。

将 LDAP 组与角色名称或“外部身份认证 ID”值进行匹配时,BookStack 会将 LDAP 组名统一转换为小写,并将空格替换为连字符。例如,要匹配名为“United Kingdom”的 LDAP 组,可将“外部身份认证 ID”值设为“united-kingdom”。

此功能要求 LDAP 服务器在收到查询时能够返回用户组。ActiveDirectory 默认通过“memberOf”属性启用该功能,但其他 LDAP 系统可能需要手动配置。请注意,“memberOf”属性不包含用户的主组。如果使用 OpenLDAP,则需要配置 memberof overlay。

启用组同步需要将以下设置添加到 .env 文件中:

# Enable LDAP group sync, Set to 'true' to enable.
LDAP_USER_TO_GROUPS=true

# LDAP user attribute containing groups, Defaults to 'memberOf'.
LDAP_GROUP_ATTRIBUTE="memberOf"

# Remove users from roles that don't match LDAP groups.
# Note: While this is enabled the "Default Registration Role", editable within the 
# BookStack settings view, will be considered a matched role and assigned to the user.
LDAP_REMOVE_FROM_GROUPS=false

# If you need to debug the group details coming from your LDAP server, add the below and uncomment (remove the #).
# Only set this option if debugging since it will block logins and potentially show private details.
#LDAP_DUMP_USER_GROUPS=true