- application.conf中配置https端口和证书相关信息
ktor {
deployment {
port = 8080
sslPort = 8443
watch = ["xxxServer"]
}
application {
modules = [com.cxyzy.xxx.ApplicationKt.module]
}
security {
ssl {
keyStore = build/temporary.jks
keyAlias = mykey
keyStorePassword = changeit
privateKeyPassword = changeit
}
}
}
- 生成证书 可以手工生成证书或找CA证书厂家申请(商用时推荐采用,PS:阿里云上可以申请免费DV SSL证书). 本例图省事,直接在代码中生成证书(生成证书可以用keytool等命令,也可以用我写的kotlin工程).
- build.gradle的dependencies中增加:
compile "io.ktor:ktor-network-tls:$ktor_version"
- 启动函数中增加生成证书:
fun main(args: Array) {
// generate SSL certificate
val file = File("build/temporary.jks")
if (!file.exists()) {
file.parentFile.mkdirs()
generateCertificate(file)
}
io.ktor.server.netty.EngineMain.main(args)
}
- 测试 如果采用postman,则需要关闭postman的证书校验开关: 在Preferences菜单的General页签中关闭“SSL certificate verification”
正常情况下,应该禁用http,仅启用https.但是发现ktor的单元测试时,只能通过http,所以单元测试时需要允许http.
参考资料https://ktor.io/quickstart/guides/ssl.html https://ktor.io/servers/self-signed-certificate.html#artifact-75
安卓开发技术分享: https://blog.csdn.net/yinxing2008/article/details/84555061 点击关注专栏,查看最新技术分享 更多技术总结好文,请关注:「程序园中猿」