无法在Ktor项目中引入ktor-server-auth库,求解决依赖错误
Ktor引入ktor-server-auth依赖编译失败问题解决
问题描述
尝试在Ktor项目中引入ktor-server-auth库实现用户认证,依赖配置为implementation("io.ktor:ktor-server-auth:$ktor_version"),但编译时出现如下错误:
Execution failed for task ':compileKotlin'. > Could not resolve all files for configuration ':compileClasspath'. > Could not resolve io.ktor:ktor-server-auth:1.5.1. Required by: project : > Could not resolve io.ktor:ktor-server-auth:1.5.1. > Could not get resource 'https://kotlin.bintray.com/ktor/io/ktor/ktor-server-auth/1.5.1/ktor-server-auth-1.5.1.pom'. > Could not HEAD 'https://kotlin.bintray.com/ktor/io/ktor/ktor-server-auth/1.5.1/ktor-server-auth-1.5.1.pom'. > Certificate for <kotlin.bintray.com> doesn't match any of the subject alternative names: [*.jfrog.io, *.int.jfrog.io, jfrog.io, *.pe.jfrog.io] * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
项目配置文件
build.gradle
buildscript { repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'kotlin' apply plugin: 'application' group 'com.example' version '0.0.1' mainClassName = "io.ktor.server.netty.EngineMain" sourceSets { main.kotlin.srcDirs = main.java.srcDirs = ['src'] test.kotlin.srcDirs = test.java.srcDirs = ['test'] main.resources.srcDirs = ['resources'] test.resources.srcDirs = ['testresources'] } repositories { mavenLocal() mavenCentral() jcenter() maven { url = uri('https://kotlin.bintray.com/ktor') } maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") } maven { url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "io.ktor:ktor-server-netty:$ktor_version" implementation "ch.qos.logback:logback-classic:$logback_version" implementation "io.ktor:ktor-server-core:$ktor_version" implementation "io.ktor:ktor-freemarker:$ktor_version" implementation "io.ktor:ktor-server-host-common:$ktor_version" testImplementation "io.ktor:ktor-server-tests:$ktor_version" implementation("org.jetbrains.exposed:exposed:0.12.1") // H2 in-memory database implementation("com.h2database:h2:1.4.191") // Exposed depends on Joda-time so include it implementation("joda-time:joda-time:2.9.2") implementation 'com.google.code.gson:gson:2.8.5' //session implementation("io.ktor:ktor-server-sessions:$ktor_version") // Auth implementation "io.ktor:ktor-server-auth:$ktor_version" }
gradle.properties
ktor_version=1.5.1 kotlin.code.style=official kotlin_version=1.4.21 logback_version=1.2.1 exposed_version = 0.41.1 h2_version = 2.1.214
问题原因
错误日志显示kotlin.bintray.com的证书不匹配对应域名,核心原因是Bintray服务已停止运营,JetBrains早已将Ktor的仓库迁移至Maven Central和JetBrains Space仓库,旧的https://kotlin.bintray.com/ktor仓库无法正常访问,导致依赖解析失败。此外jcenter()服务也已停止,继续保留会增加后续依赖解析风险。
解决方案
步骤1:移除无效仓库
在build.gradle的repositories块中,删除以下两行无效仓库配置:
jcenter() maven { url = uri('https://kotlin.bintray.com/ktor') }
步骤2:调整仓库配置
对于Ktor 1.5.x版本,保留以下有效仓库即可:
repositories { mavenLocal() mavenCentral() maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") } maven { url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") } }
步骤3:同步并重新编译
执行Gradle同步操作,之后重新编译项目,依赖即可正常解析。
额外建议:考虑升级Ktor到2.x系列稳定版本,新版本不仅修复了更多问题,还提供了更完善的认证API,且完全基于Maven Central仓库,彻底规避旧仓库访问问题。
内容的提问来源于stack exchange,提问作者monjur
相关产品推荐
相关产品推荐

