【问题标题】:How to enable spring security kotlin DSL?如何启用 Spring Security kotlin DSL?
【发布时间】:2021-05-07 03:03:56
【问题描述】:

我们如何启用对 spring security kotlin DSL 的支持?

从 IDE (IntelliJ) 的屏幕截图中可以看出,DSL 不可用:

这是完整的SecurityConfig.kt 文件:

package com.example.backend.core

import org.springframework.context.annotation.Bean
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
import org.springframework.security.config.web.server.ServerHttpSecurity
import org.springframework.security.web.server.SecurityWebFilterChain

@EnableWebFluxSecurity
class SecurityConfig {

  @Bean
  fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
    return http {
      csrf { disable() }
      formLogin { disable() }
      httpBasic { disable() }
      // ...
    }
  }

}

这是我们的build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val springBootVersion = "2.4.2"

plugins {
    id("org.springframework.boot") version "2.4.2"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.4.21"
    kotlin("plugin.spring") version "1.4.21"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

configurations {
    compileOnly {
        extendsFrom(configurations.annotationProcessor.get())
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
    implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.flywaydb:flyway-core")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("io.micrometer:micrometer-registry-prometheus")
    runtimeOnly("org.postgresql:postgresql")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("org.springframework.security:spring-security-test")
}

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

tasks.withType<Test> {
    useJUnitPlatform()
}

这是 intellij 版本:

IntelliJ IDEA 2020.3 (Community Edition)
Build #IC-203.5981.155, built on November 30, 2020
Runtime version: 11.0.9+11-b1145.21 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.4.0-64-generic
GC: ParNew, ConcurrentMarkSweep
Memory: 1979M
Cores: 12
Registry: compiler.automake.allow.when.app.running=true
Non-Bundled Plugins: Key Promoter X, Dart, io.flutter, Lombook Plugin, org.jetbrains.kotlin
Current Desktop: X-Cinnamon

我们错过了一些依赖吗?它是否需要任何特定的 intellij 设置?

【问题讨论】:

  • 你能在你的SecurityConfig 类中分享导入吗?我怀疑它正在导入不正确的方法。
  • 应该导入哪一个? ide不建议任何。当我稍后回家时,我可以分享当前的导入。
  • @EleftheriaStein-Kousathana 我将文件SecurityConfig.kt 的全部内容添加到问题中(包括导入)。

标签: spring-boot kotlin intellij-idea spring-security


【解决方案1】:

您需要手动导入ServerHttpSecurityinvoke

import org.springframework.security.config.web.server.invoke

由于 1.4 中的 this Kotlin issue,IDE 并未按应有的方式向您建议。

这计划在 Kotlin 1.4.30 中修复。

【讨论】:

  • 谢谢,那行得通。:) 我还看到不是disabled(),而是disable()
  • 我使用的是 1.4.32 并没有提示。
  • 我也在使用 Kotlin 1.5.20 和相同版本的 Spring 插件,但它仍然无法正常工作。
【解决方案2】:

我注意到您的问题是关于 WebFlux,但只是补充 @Eleftheria 的答案。

对于WebMvc 的人,您应该导入:

import org.springframework.security.config.web.servlet.invoke

(servlet 与服务器)

【讨论】:

    【解决方案3】:

    这个 DSL 从 Spring Security 5.3 版本开始内置。例如org.springframework.security:spring-security-config:5.3.4.RELEASE 库有它:org.springframework.security.config.web.servlet.HttpSecurityDsl#csrf 并且例如

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.3.RELEASE'
    

    库将包含它。

    【讨论】:

    • 如图所示,我们使用的是 Spring Boot 2.4.2 版本,它包含 spring-security-config:5.4.2,所以 DSL 应该可以工作,但不知何故它不是
    • 课程HttpSecurityDsl 可用。由于它位于servlet 文件夹中,我现在想知道它是否在反应堆栈中不可用?我们使用spring-boot-starter-webflux,因此ServerHttpSecurityDsl 应该适用吗?它也在类路径中
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2020-07-24
    • 2020-05-29
    • 2018-02-06
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多