Search code examples
javakotlingradlespring-securitybuild.gradle

Unresolved reference: WebSecurityConfigurerAdapter with spring-boot-starter-security Gradle/Kotlin


I am new to using Gradle and Kotlin, and I am trying to set up a simple API key authentication for my Spring Boot application. I added spring-boot-starter-security to my build.gradle.kts file, but in my SecurityConfig file, the import for

org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter is unresolved. The other security imports are fine. For example,

org.springframework.security.config.annotation.web.builders.HttpSecurity works just fine. I am using Intellij, and I thought maybe it was a caching issue, but it has persisted after invalidating cache, as well.

Here is my build.gradle.kts file.

plugins {
    id("org.springframework.boot") version "3.0.6"
    id("io.spring.dependency-management") version "1.1.0"
    id("io.freefair.lombok") version "8.0.1"
    kotlin("jvm") version "1.7.22"
    kotlin("plugin.spring") version "1.7.22"
}

group = "com.demo"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.springframework.boot:spring-boot-starter-security")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.security:spring-security-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

I am referencing this medium article: https://medium.com/techwasti/enable-spring-security-using-kotlin-6b9abb36d218

My SecurityConfig file is under src/main/kotlin/com.demo.dashboard/config.

I tried restarting the IDE, referencing the spring security dependency in different ways in the build.gradle.kts file, and re-initializing the project with the dependency selected from the start, but I have had no luck.


Solution

  • Looks like WebSecurityConfigurerAdapter has been deprecated last year, in Spring Security 5.7.

    From quick search in the java doc, the class no longer exists in 6.0.3 (latest version). From the build.gradle file you posted, you are using the latest version.

    The article under first link describes alternative approaches to configuring spring security. This article may also be helpful.