LDAP
必需依赖项:io.ktor:ktor-server-auth、io.ktor:ktor-server-auth-ldap
代码示例: auth-ldap
Native 服务器Ktor 支持 Kotlin/Native,并允许您在无需额外运行时或虚拟机的情况下运行服务器。支持:✖️
LDAP 是一种用于与各种可存储用户信息的目录服务配合使用的协议。Ktor 允许您使用 basic、digest 或基于表单的身份验证方案来验证 LDAP 用户。
您可以在 Ktor Server 中的身份验证与授权部分获取关于 Ktor 身份验证和授权的常规信息。
添加依赖项
要启用 LDAP 身份验证,您需要在构建脚本中包含 ktor-server-auth 和 ktor-server-auth-ldap 构件:
implementation("io.ktor:ktor-server-auth:$ktor_version")
implementation("io.ktor:ktor-server-auth-ldap:$ktor_version")implementation "io.ktor:ktor-server-auth:$ktor_version"
implementation "io.ktor:ktor-server-auth-ldap:$ktor_version"<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-auth</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-auth-ldap</artifactId>
<version>${ktor_version}</version>
</dependency>配置 LDAP
第 1 步:选择身份验证提供程序
要对 LDAP 用户进行身份验证,首先需要选择用于用户名和密码验证的身份验证提供程序。在 Ktor 中,可以使用 basic、digest 或基于表单的提供程序。例如,若要使用 basic 身份验证提供程序,请在 install 块中调用 basic 函数。
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.auth.ldap.*
//...
install(Authentication) {
basic {
validate { credentials ->
// 对 LDAP 用户进行身份验证
}
}
}validate 函数将用于检查用户凭据。
第 2 步:验证 LDAP 用户身份
要验证 LDAP 用户身份,需要调用 ldapAuthenticate 函数。该函数接收 UserPasswordCredential 并根据指定的 LDAP 服务器对其进行验证。
install(Authentication) {
basic("auth-ldap") {
validate { credentials ->
val url = "ldap://0.0.0.0:389"
val userDNFormat = "cn=%s,dc=ktor,dc=io"
ldapAuthenticate(credentials, url, userDNFormat)
}
}
}在身份验证成功时,validate 函数将返回 UserIdPrincipal;若验证失败,则返回 null。
(可选)您可以为已通过身份验证的用户添加额外的验证逻辑。
install(Authentication) {
basic("auth-ldap") {
validate { credentials ->
val url = "ldap://localhost:389"
val userDNFormat = "cn=%s,dc=ktor,dc=io"
ldapAuthenticate(credentials, url, userDNFormat) {
if (it.name == it.password) {
UserIdPrincipal(it.name)
} else {
null
}
}
}
}
}类型安全身份验证方案 API 目前为实验性功能。它可能会在任何时候被移除或更改。 需要明确选择启用(Opt-in)。有关更多详细信息,请参阅 类型安全身份验证文档。
目前没有专用于 LDAP 的类型安全身份验证方案。请在类型安全 basic、digest 或 form 方案的 validate 块内调用 ldapAuthenticate。
向 ldapAuthenticate 传递一个代码块以返回自定义的 Principal 类型:
data class User(val name: String)
val ldapAuth = basic<User>("auth-ldap") {
validate { credentials ->
val url = "ldap://0.0.0.0:389"
val userDNFormat = "cn=%s,dc=ktor,dc=io"
ldapAuthenticate(credentials, url, userDNFormat) {
User(it.name)
}
}
}该代码块还允许您为已通过身份验证的用户添加校验:
val ldapAuth = basic<User>("auth-ldap") {
validate { credentials ->
val url = "ldap://localhost:389"
val userDNFormat = "cn=%s,dc=ktor,dc=io"
ldapAuthenticate(credentials, url, userDNFormat) {
if (it.name == it.password) {
User(it.name)
} else {
null
}
}
}
}如需了解完整的 API,请参阅类型安全身份验证。
第 3 步:保护特定资源
配置好 LDAP 后,可以使用 authenticate 函数来保护应用程序中的特定资源。在身份验证成功的情况下,可以在路由处理程序中使用 call.principal 函数检索已验证的 UserIdPrincipal,并获取已验证用户的名称。
routing {
authenticate("auth-ldap") {
get("/") {
val user = call.principal<UserIdPrincipal>()
call.respondText("Hello, ${user?.name}!")
}
}
}将该方案传递给 authenticateWith()。在该代码块内,call.principal 即为您的 Principal 类型,且绝不会为 null,因此不需要进行类型转换或 null 检查:
routing {
authenticateWith(ldapAuth) {
get("/") {
call.respondText("Hello, ${call.principal.name}!")
}
}
}可以在此处找到完整的可运行示例:auth-ldap。
请注意,当前的 LDAP 实现是同步的。
