Spring Boot SecurityFilterChain方法Sping Boot 3.1.5Kotlin

omhiaaxx  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(128)

我正在尝试使用Sping Boot 3.1.5创建securityFilterChain方法
这就是我所拥有的:

@Bean
@Throws(java.lang.Exception::class)
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain? {
    http
        .authorizeHttpRequests(
            Customizer<AuthorizationManagerRequestMatcherRegistry> { authorizeHttpRequests: AuthorizationManagerRequestMatcherRegistry ->
                authorizeHttpRequests
                    .requestMatchers("/**").hasRole("USER")
            }
        )
    return http.build()
}

字符串
不幸的是,我得到了这个编译错误:“应该为外部类指定类型参数。使用完整的类名来指定它们。”
有没有人能给我指引正确的方向?
以下是我尝试过的其他方法:

@Bean
@Throws(Exception::class)
     fun filterChain(http: HttpSecurity): SecurityFilterChain {
         http.authorizeHttpRequests((requests) -> requests
        .requestMatchers("/users/registration").permitAll())
        return http.build()
     }


但这并不能很好地编译。
以下是项目依赖项:

dependencies {
    implementation("org.apache.tomcat.embed:tomcat-embed-jasper")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.security:spring-security-core")
    implementation("org.springframework.security:spring-security-config")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("io.jsonwebtoken:jjwt:0.9.0")
    implementation("org.postgresql:postgresql")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
         exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
    testImplementation("org.springframework.security:spring-security-test")
}

egdjgwm8

egdjgwm81#

该文档包括Kotlin配置的概述:

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

@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  http {
    authorizeRequests {
      authorize(anyRequest, authenticated)
    }
    formLogin { }
    httpBasic { }
  }
  return http.build()
}

字符串
此示例使用了KotlinDSL,并包含了HttpSecurityinvoke扩展函数的导入,这是以这种方式使用DSL所必需的。
还要注意的是,这个例子使用了authorizeRequests,但建议使用authorizeHttpRequests。我在文档中使用了filed an issue来改进这个片段。

相关问题