如何启用 Spring Security kotlin DSL?

Stu*_*uck 9 intellij-idea spring-security kotlin spring-boot

我们如何启用对 spring 安全 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() }
      // ...
    }
  }

}

Run Code Online (Sandbox Code Playgroud)

这是我们的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()
}
Run Code Online (Sandbox Code Playgroud)

这是 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
Run Code Online (Sandbox Code Playgroud)

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

alb*_*nte 15

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

对于WebMvc人们来说,你应该导入:

import org.springframework.security.config.web.servlet.invoke
Run Code Online (Sandbox Code Playgroud)

(Servlet 与服务器)

  • 在 spring security 6(spring boot 3/spring6) 中,使用 import org.springframework.security.config.annotation.web.invoke 代替。 (7认同)

Ele*_*ana 10

您需要手动导入ServerHttpSecurity invoke.

import org.springframework.security.config.web.server.invoke
Run Code Online (Sandbox Code Playgroud)

由于1.4 中的这个 Kotlin 问题,IDE 并没有像它应该的那样向您建议。

这计划在 Kotlin 1.4.30 中修复。

  • 12 个月后,我正在使用 1.6.10 最新的稳定版本,但仍然无法自动导入它,需要通过 spring security 的 hello world 示例来获取此导入,duh (4认同)
  • 我用的是1.4.32,没有提示。 (3认同)
  • 我也在使用 Kotlin 1.5.20 和同一版本的 Spring 插件,但它仍然无法工作。 (2认同)

Rai*_*rim 9

对我有用的是显式调用对象.invoke上的方法HttpSecurity

http.invoke {
    csrf { disable }
}
Run Code Online (Sandbox Code Playgroud)

这将强制我的 IDE(我使用 IntelliJ)导入.invoke.