将Spring Security从5迁移到6

9njqaruj  于 2022-12-04  发布在  Spring
关注(0)|答案(1)|浏览(345)

我们目前拥有:

@Configuration
@EnableWebSecurity
open class OpenApiSecurityConfig() : WebSecurityConfigurerAdapter() {
    override fun configure(http: HttpSecurity) {
            http.requestMatchers()
                .antMatchers("/docs")
                .and()
                .addFilterBefore(MyApiFilter(), FilterSecurityInterceptor::class.java)
    }
}

根据文档,应将其替换为SecurityFilterChain bean:

@Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
            http.requestMatchers()
                .antMatchers("/docs")
                .and()
                .addFilterBefore(MyApiFilter(), AuthorizationFilter::class.java)
    }

然而,HttpSecurity对象已经完全更改,requestMatchers()丢失,我找不到如何执行迁移的规范。

fv2wmkja

fv2wmkja1#

您可以使用securityMatcher("/docs")来应用筛选器
允许将HttpSecurity配置为仅在与提供的模式匹配时调用。如果类路径中有Spring MVC,则此方法创建MvcRequestMatcher,否则创建AntPathRequestMatcher。如果需要更高级的配置,请考虑使用securityMatchers(定制器)或securityMatcher(RequestMatcher)

@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
   return http
            .securityMatcher("/docs") 
            .addFilterBefore(MyApiFilter(), AuthorizationFilter::class.java)
            .build()
}

相关问题