LDAP
必要相依性: io.ktor:ktor-server-auth, io.ktor:ktor-server-auth-ldap
程式碼範例: auth-ldap
原生伺服器Ktor 支援 Kotlin/Native,可讓您在無需額外執行階段或虛擬機的情況下執行伺服器。支援: ✖️
LDAP 是一種用於處理各種可儲存使用者資訊之目錄服務的協定。Ktor 允許你使用 basic、digest 或 form-based 認證配置來驗證 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 或 form-based 提供者來完成此操作。例如,若要使用 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 實作是同步的。
