I’m opting for Gradle Kotlin DSL as the typical choice for Ktor samples. I need to keep things minimal but ensure a complete skeleton matches the user's request for a boilerplate. I'll list the necessary dependencies in build.gradle.kts, including Ktor libraries and logging.
Next, I’ll outline the project structure, detailing the main application file and additional modules, focusing on routing and authentication setup. I might also include optional serialization steps in one of the plugins. It’s all about clarity!Configuring JWT in Ktor
In JwtConfig.kt, I'll centralize the configuration and token generation. The login route will implement a simple user check, expecting JSON with a username and password. If the details are "test" for both, it will respond with a token.
I need to keep in mind potential pitfalls with KotlinX serialization and content negotiation. For Ktor 2.3+, I could define versions right in build.gradle.kts, like setting ktor_version = "2.3.12" and ensuring the Kotlin version is stable, assuming everything checks out. I'm on it!Designing the Ktor Plugin
I’m getting into designing the plugin with Serialization.kt. I'll set up serialization by installing Content Negotiation with JSON support. For routing, I’m defining data classes for LoginRequest and LoginResponse, setting endpoints for the root and login.
I realize I need to call configureSerialization() and configureSecurity() before configureRouting(). In the /login endpoint, I'll validate user credentials and respond with a token or an unauthorized error. Also, in the /protected route, I’ll retrieve the username using the JWTPrincipal. I’ll double-check the syntax to use the correct typed extensions in Ktor 2.0!Setting Up JWT Security
In the code, I have two options for getting the username from JWTPrincipal: val username = principal.payload.getClaim("username").asString() or using the getClaim method directly. I’ll choose the safer option with payload for clarity.
Moving to Security.kt, I need to set up JWT configurations. I have to import JWTVerifier and Date, with a validity property set to 10 hours. The configureSecurity() function will install JWT authentication, verifying tokens and handling unauthorized responses. I'll ensure all necessary imports are included!Setting Up the Main Application
I’m working on the main application in Ktor. The main function will start an embedded server on port 8080, and I’ll define a module function to configure serialization, security, and routing.
I should consider adding environment variable injection for the JWT secret, like val secret = System.getenv("JWT_SECRET") ?: "dev-secret" and comment on customizing this secret.
For the final response, I’ll keep it concise and focus on the code. I’ll include a quick "How to run" section along with instructions to test using curl. Let's get this finalized! Below is a minimal Ktor 2.x boilerplate server with JWT authentication: a /login route that issues tokens and a /protected route that requires a valid JWT.
1. build.gradle.kts
kotlin
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val ktor_version = "2.3.12"
val kotlin_version = "2.0.21"
val logback_version = "1.5.12"
val jwt_version = "4.4.0"
plugins {
kotlin("jvm") version "2.0.21"
application
}
group = "com.example"
version = "0.0.1"
application {
mainClass.set("com.example.ApplicationKt")
}
repositories {
mavenCentral()
}
dependencies {
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
// Auth + JWT
implementation("io.ktor:ktor-server-auth-jvm:$ktor_version")
implementation("io.ktor:ktor-server-auth-jwt-jvm:$ktor_version")
implementation("com.auth0:java-jwt:$jwt_version")
// JSON
implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktor_version")
// Logging
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}
This is a minimal, production-extensible boilerplate; you can plug in a real user store, move secrets to configuration, and add refresh tokens or roles/permissions as needed.