認証

Estimated reading time: 1 minute

Ktorクライアントは、標準のプラグイン可能な機能として、すぐに使用できる認証をサポートしています。

This feature is defined in the class io.ktor.client.features.auth.Auth in the artifact io.ktor:ktor-client-auth:$ktor_version.
dependencies { implementation "io.ktor:ktor-client-auth:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-auth:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-auth</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
dependencies { implementation "io.ktor:ktor-client-auth-jvm:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-auth-jvm:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-auth-jvm</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
dependencies { implementation "io.ktor:ktor-client-auth-native:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-auth-native:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-auth-native</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
dependencies { implementation "io.ktor:ktor-client-auth-js:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-auth-js:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-auth-js</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>

Installation

val client = HttpClient() {
    install(Auth) {
        // providers config
        ...
    }
}

Providers

Basic

このProviderはAuthorization: Basicを特定のクレデンシャルとともに送信します。

val client = HttpClient() {
    install(Auth) {
        basic {
            username = "username"
            password = "password"
        }
    }
}

この機能はIETFのRFC 7617を実装しています。

Digest

このProviderはAuthorization: Digestを特定のクレデンシャルとともに送信します。

val client = HttpClient() {
    install(Auth) {
        digest {
            username = "username"
            password = "password"
            realm = "custom"
        }
    }
}

この機能はIETFのRFC 2617を実装しています。