Skip to content

Ktor 客户端中的摘要认证

必需的依赖项: io.ktor:ktor-client-auth

代码示例: client-auth-digest

在摘要认证方案中,散列函数在将用户名和密码通过网络发送之前应用于它们。

在服务器端,Ktor 提供了 Authentication 插件用于处理摘要认证。

摘要认证流程

摘要认证流程如下:

  1. 客户端向服务器应用程序中的特定资源发出不带 Authorization 头部信息的请求。

  2. 服务器向客户端响应 401(未授权)响应状态,并使用 WWW-Authenticate 响应头部提供信息,表明该路由受摘要认证方案保护。典型的 WWW-Authenticate 头部如下所示:

    WWW-Authenticate: Digest
            realm="Access to the '/' path",
            nonce="e4549c0548886bc2",
            algorithm="MD5"
  3. 通常,客户端会显示一个登录对话框,用户可以在其中输入凭据。然后,客户端会发出带有以下 Authorization 头部信息的请求:

    Authorization: Digest username="jetbrains",
            realm="Access to the '/' path",
            nonce="e4549c0548886bc2",
            uri="/",
            algorithm=MD5,
            response="6299988bb4f05c0d8ad44295873858cf"

    response 值的生成方式如下:

    a. HA1 = MD5(username:realm:password)

    b. HA2 = MD5(method:digestURI)

    c. response = MD5(HA1:nonce:HA2)

  4. 服务器验证客户端发送的凭据并响应请求的内容。

配置摘要认证

要在 Authorization 头部中使用 Digest 方案发送用户凭据,您需要按如下方式配置 digest 认证提供者:

  1. install 代码块内调用 digest 函数。
  2. 使用 DigestAuthCredentials 提供所需的凭据,并将此对象传递给 credentials 函数。
  3. 可选地,使用 realm 属性配置 realm。
kotlin
val client = HttpClient(CIO) {
    install(Auth) {
        digest {
            credentials {
                DigestAuthCredentials(username = "jetbrains", password = "foobar")
            }
            realm = "Access to the '/' path"
        }
    }
}

您可以在此处找到完整示例:client-auth-digest