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

How to exclude a path from authentication in a spring based reactive application?

在非反应性 spring 应用程序中,我通常会创建一个配置 class,扩展 WebSecurityConfigurerAdapter 并像这样配置 WebSecurity

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

如何在响应式应用程序中执行等效操作?

在您用 @EnableWebFluxSecurity@EnableReactiveMethodSecurity 注释的安全配置 class 中,按如下方式注册一个 bean:

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

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