ktor配置https证书

作者: 蓝不蓝编程 | 来源:发表于2019-05-22 20:02 被阅读0次

配置步骤

  1. 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
    }
  }
}
  1. 生成证书
    可以手工生成证书或找CA证书厂家申请(商用时推荐采用,PS:阿里云上可以申请免费DV SSL证书).
    本例图省事,直接在代码中生成证书.
  1. build.gradle的dependencies中增加:
compile "io.ktor:ktor-network-tls:$ktor_version"
  1. 启动函数中增加生成证书:
fun main(args: Array<String>) {
    // generate SSL certificate
    val file = File("build/temporary.jks")
    if (!file.exists()) {
        file.parentFile.mkdirs()
        generateCertificate(file)
    }
    io.ktor.server.netty.EngineMain.main(args)
}
  1. 测试
    如果采用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://www.jianshu.com/p/442339952f26
点击关注专辑,查看最新技术分享
更多技术总结好文,请关注:「程序园中猿」

相关文章

网友评论

    本文标题:ktor配置https证书

    本文链接:https://www.haomeiwen.com/subject/iskpzqtx.html