【问题标题】:How to configure ssl using KTOR library?如何使用 KTOR 库配置 ssl?
【发布时间】:2020-02-23 00:27:30
【问题描述】:

我正在寻找一种为 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()

【问题讨论】:

    标签: kotlin ktor


    【解决方案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() {
    
    }
    
    

    【讨论】:

      猜你喜欢
      • 2019-05-22
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 2021-03-01
      • 2021-06-04
      • 2019-03-04
      • 1970-01-01
      相关资源
      最近更新 更多