如何使用 KTOR 库配置 SSL?

7

我正在寻找一种配置 Ktor 应用程序的 https 的方法。

我在这里找到了官方文档: https://ktor.io/servers/self-signed-certificate.html 其中解释了如何在 HOCON 配置文件中添加证书链接。

是否有可能不使用配置文件来配置 SSL?

这是我的代码库:

http = embeddedServer(Netty, port = listenPort, configure = {
                connectionGroupSize = 1
                workerGroupSize = 5
            }){
                if(sslCertificate != null) {
                    install(HttpsRedirect) {
                        sslPort = 443
                    }
                }

                install(StatusPages) {
                    exception<NotFoundError> { cause ->
                        logger.error("NotFoundError:", cause.message)
                        call.respondText(cause.message ?: "",
                                ContentType.Text.Plain, HttpStatusCode.NotFound){}
                    }
                    exception<BadFormatError> { cause ->
                        logger.error("BadFormatError:", cause.message)
                        call.respondText(cause.message ?: "",
                                ContentType.Text.Plain, HttpStatusCode.BadRequest){}
                    }
                    exception<UserMistake> { cause ->
                        logger.error("UserMistake:", cause.message)
                        call.respondText(cause.message ?: "",
                                ContentType.Text.Plain, HttpStatusCode.BadRequest){}
                    }
                    exception<OverloadedException> { cause ->
                        logger.error("OverloadedException:", cause.message)
                        call.respondText(cause.message ?: "",
                                ContentType.Text.Plain, HttpStatusCode.ServiceUnavailable){}
                    }
                    exception<Exception> { cause ->
                        logger.error("Exception:", cause.message)
                        call.respondText(cause.message ?: "",
                                ContentType.Text.Plain, HttpStatusCode.InternalServerError){}
                    }
                }
                intercept(ApplicationCallPipeline.Call) {
                    call.response.headers.append(HttpHelper.ACCESS_CONTROL_ALLOW_ORIGIN, "*")
                    call.response.headers.append(HttpHelper.ACCESS_CONTROL_REQUEST_METHOD, "POST, GET, OPTIONS")
                   // call.response.headers.append(HttpHelper.CONTENT_TYPE, "application/json")
                    if(call.request.uri.endsWith("/")) {
                        call.respondRedirect(call.request.uri.dropLast(1))
                    }
                }
            }
            http.start()
1个回答

1

这有点复杂,但是可以实现。您可以在环境中使用sslConnector进行配置:


fun main() {
    val environment = applicationEngineEnvironment {
        log = LoggerFactory.getLogger("ktor.application")

        // Here you can the key store and keys configuration
        sslConnector(keyStore, ...)

        module(Application::myModule)
    }

    embeddedServer(Netty, environment, configure = {
        // ...
    })
}

fun Application.myModule() {

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接