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

huangapple go评论75阅读模式
英文:

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.

英文:

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(&quot;org.springframework.boot&quot;) version &quot;3.0.6&quot;
	id(&quot;io.spring.dependency-management&quot;) version &quot;1.1.0&quot;
	id(&quot;io.freefair.lombok&quot;) version &quot;8.0.1&quot;
	kotlin(&quot;jvm&quot;) version &quot;1.7.22&quot;
	kotlin(&quot;plugin.spring&quot;) version &quot;1.7.22&quot;
}

group = &quot;com.demo&quot;
version = &quot;0.0.1-SNAPSHOT&quot;
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
	mavenCentral()
}

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

tasks.withType&lt;KotlinCompile&gt; {
	kotlinOptions {
		freeCompilerArgs = listOf(&quot;-Xjsr305=strict&quot;)
		jvmTarget = &quot;17&quot;
	}
}

tasks.withType&lt;Test&gt; {
	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.

答案1

得分: 0

WebSecurityConfigurerAdapter 看起来在 Spring Security 5.7 中已被弃用,详见此链接

从您提供的 build.gradle 文件来看,您正在使用最新版本,但是从 java doc 中的查询来看,该类在 6.0.3(最新版本)中已不再存在,详见此链接

第一个链接下的文章描述了配置 Spring Security 的替代方法,这篇文章 也可能会有所帮助。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月10日 13:58:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定