Kotlin 如何使用KTOR库配置ssl?

Kotlin 如何使用KTOR库配置ssl?,kotlin,ktor,Kotlin,Ktor,我正在寻找一种为Ktor应用程序配置https的方法 我在那里找到了一份官方文件: 这里解释了如何在HOCON配置文件中添加指向证书的链接 是否可以在没有配置文件的情况下配置ssl 以下是我的代码库: http = embeddedServer(Netty, port = listenPort, configure = { connectionGroupSize = 1 workerGroupSize = 5

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

我在那里找到了一份官方文件: 这里解释了如何在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()
http=embeddedServer(Netty,port=listenPort,configure={
connectionGroupSize=1
workerGroupSize=5
}){
如果(sslCertificate!=null){
安装(HttpsRedirect){
sslPort=443
}
}
安装(状态页){
异常{原因->
logger.error(“NotFoundError:,cause.message”)
call.respondText(cause.message?:“”,
ContentType.Text.Plain,HttpStatusCode.NotFound){}
}
异常{原因->
logger.error(“BadFormatError:,cause.message”)
call.respondText(cause.message?:“”,
ContentType.Text.Plain,HttpStatusCode.BadRequest){}
}
异常{原因->
logger.error(“usermission:,cause.message”)
call.respondText(cause.message?:“”,
ContentType.Text.Plain,HttpStatusCode.BadRequest){}
}
异常{原因->
logger.error(“OverloadeException:,cause.message”)
call.respondText(cause.message?:“”,
ContentType.Text.Plain,HttpStatusCode.ServiceUnavailable){}
}
异常{原因->
logger.error(“异常:,原因.消息”)
call.respondText(cause.message?:“”,
ContentType.Text.Plain,HttpStatusCode.InternalServerError){}
}
}
拦截(ApplicationCallPipeline.Call){
call.response.headers.append(HttpHelper.ACCESS\u CONTROL\u ALLOW\u ORIGIN,“*”)
call.response.headers.append(HttpHelper.ACCESS\u CONTROL\u REQUEST\u方法,“POST、GET、OPTIONS”)
//call.response.headers.append(HttpHelper.CONTENT_类型,“application/json”)
if(call.request.uri.endsWith(“/”)){
call.respondRedirect(call.request.uri.dropLast(1))
}
}
}
http.start()

这有点复杂,但也有可能。您可以在以下环境中使用
sslConnector
配置:


主要内容(){
val环境=应用程序工程环境{
log=LoggerFactory.getLogger(“ktor.application”)
//您可以在这里查看密钥存储和密钥配置
sslConnector(密钥库,…)
模块(应用程序::myModule)
}
嵌入式服务器(网络、环境、配置={
// ...
})
}
有趣的应用程序。myModule(){
}