我们如何支持spring security kotlin DSL?

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

enter image description here

This is the full SecurityConfig.kt file:

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() }
      // ...
    }
  }

}

This is our 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()
}

And this is the intellij version:

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设置?

推荐答案

您需要手动导入ServerHttpSecurity invoke.

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

由于1.4中的this Kotlin issue,IDE并没有向您推荐它.

这将在Kotlin 1.4.30中修复.

Kotlin相关问答推荐

Compose:LaunchedEffect在密钥更改后不会重新启动

如何在使用Kotlin Coroutines时检测和记录何时出现背压

在Mapstruct中重用@映射定义

按钮无法在 Android Studio 上打开新活动

kotlin 父类具有依赖于抽象变量的变量

判断 Kotlin 变量是否为函数

如何在 jOOQ 中两次加入同一张表?

将子元素放在一个列表中

你怎么知道什么时候需要 yield()?

在 kotlin 中模拟伴随对象函数

Kotlin RxJava 可空的错误

Spring webflux bean验证不起作用

如果 Maybe 完成,则从其他来源将 Maybe 转换为 Single

在调用Kotlin数据类中的超类构造函数之前访问函数

在Kotlin中传递并使用函数作为构造函数参数

未找到导入 kotlinx.coroutines.flow.*

如何在协程之外获取 Flow 的值?

Android EditText 协程go 抖操作符,如 RxJava

Kotlin 与 C# 中的标志枚举变量具有相似效果的方式是什么

如何在 Kotlin 中定义新的运算符?