如何在基于 Spring 的反应式应用程序中从身份验证中排除路径?

jim*_*arn 11 security spring reactor

在非反应式 spring 应用程序中,我通常会创建一个配置类,扩展WebSecurityConfigurerAdapter和配置WebSecurity如下:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/pathToIgnore");
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能在反应式应用程序中做同样的事情?

Mad*_*hat 8

@EnableWebFluxSecurity在用和注释的安全配置类中@EnableReactiveMethodSecurity,注册一个 bean,如下所示:

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    return http.authorizeExchange()
        .pathMatchers("/pathToIgnore")
        .permitAll()
        .anyExchange()
        .authenticated()
        .and()
        .formLogin()
        .and()
        .csrf()
        .disable()
        .build();
}
Run Code Online (Sandbox Code Playgroud)

在此配置中,pathMatchers("/pathToIgnore").permitAll()将其配置为允许从身份验证中排除匹配的路径,并将anyExchange().authenticated()其配置为对所有其他请求进行身份验证。