Using a Self-Signed Certificate

    This function is defined in the method in the artifact io.ktor:ktor-network-tls:$ktor_version.

    dependencies { implementation "io.ktor:ktor-network-tls:$ktor_version"}

    dependencies { implementation("io.ktor:ktor-network-tls:$ktor_version")}

    <project> … <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-network-tls</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies></project>

    Table of contents:

    Since Ktor requires the certificate when it starts, you have to create the certificate before starting the server.

    One possible option is to execute the main class generating the certificate before actually running the server:

    You can declare a class with a main method that only generates the certificate when it doesn’t exist:

    build.gradle

    In your build.gradle file you can make the task to depend on a generateJks task that executes the mainclass generating the certificate. For example:

    When creating your HOCON configuration file, you have to add the ktor.deployment.sslPort, and the ktor.security.sslproperties to define the ssl port and the keyStore:

    After that you can just write a normal plain Ktor module:

    Module.kt

    Then you can point to https://127.0.0.1:8443/ to access your server.Since this is a self-signed certificate, your browser will probably warn you about an invalid certificate, soyou will have to disable that warning.

    Ktor has a full example using self-signed certificates here: