Spring security Spring security webflux中的AuthenticationManager

Spring security Spring security webflux中的AuthenticationManager,spring-security,spring-webflux,Spring Security,Spring Webflux,我正在尝试为我的SpringWebFlux应用程序构建一个自定义身份验证管理器。然而,我发现我的经理从未接到过电话。我的代码如下: @Bean SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) { return http .authorizeExchange().pathMatchers("/**").authenticated().and().httpBasic().disa

我正在尝试为我的SpringWebFlux应用程序构建一个自定义身份验证管理器。然而,我发现我的经理从未接到过电话。我的代码如下:

@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
    return http
            .authorizeExchange().pathMatchers("/**").authenticated().and().httpBasic().disable()
            .securityContextRepository(webSessionServerSecurityContextRepository())
            .addFilterAfter(new AuthenticationWebFilter(bearerTokenAuthenticationManager()),
                    SecurityWebFiltersOrder.REACTOR_CONTEXT)
            .build();

}

我做错了什么?

假设您将这个bean放在一个用
@Configuration
@EnableWebFluxSecurity
注释的类中,您的问题似乎是您没有禁用Spring Security默认配置的
csrf

您可以通过以下方法实现此目的:

@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
    return http
            .authorizeExchange().pathMatchers("/**").authenticated()
            .and()
            .httpBasic().disable()
            .csrf().disable() // Disable csrf
            .securityContextRepository(webSessionServerSecurityContextRepository())
            .addFilterAfter(new AuthenticationWebFilter(bearerTokenAuthenticationManager()),
                    SecurityWebFiltersOrder.REACTOR_CONTEXT)
            .build();

}
此外,您必须正确配置
AuthenticationWebFilter

AuthenticationWebFilter
具有以下依赖项:

…其中大多数默认情况下作为HttpBasic deps提供(从Spring Security源代码复制和粘贴):

private final ReactiveAuthenticationManagerResolver authenticationManagerResolver;
private ServerAuthenticationSuccessHandler authenticationSuccessHandler=新建WebFilterChainServerAuthenticationSuccessHandler();
私有服务器authenticationConverter authenticationConverter=新服务器HttpBasicAuthenticationConverter();
private ServerAuthenticationFailureHandler authenticationFailureHandler=新的ServerAuthenticationEntryPointFailureHandler(新的HttpBasicServerAuthenticationEntryPoint());
私有服务器securityContextRepository securityContextRepository=NoOpServerSecurityContextRepository.getInstance();//无状态会话
私有服务器WebExchangeMatcher requiresAuthenticationMatcher=ServerWebExchangeMatchers.anyExchange();
您可以使用
AuthenticationWebFilter
的setters方法设置任何需要的内容。
AuthenticationWebFilter
具有以下逻辑:


因此,根据具体情况,您必须配置一个或另一个依赖项。您可以看到一个完整的示例,说明身份验证和授权在my repo中是如何工作的:(在kotlin中,但情况相同)假设您将这个bean放在一个用
@Configuration
@EnableWebFluxSecurity
注释的类中,您的问题似乎是您没有禁用Spring Security默认配置的
csrf

您可以通过以下方法实现此目的:

@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
    return http
            .authorizeExchange().pathMatchers("/**").authenticated()
            .and()
            .httpBasic().disable()
            .csrf().disable() // Disable csrf
            .securityContextRepository(webSessionServerSecurityContextRepository())
            .addFilterAfter(new AuthenticationWebFilter(bearerTokenAuthenticationManager()),
                    SecurityWebFiltersOrder.REACTOR_CONTEXT)
            .build();

}
此外,您必须正确配置
AuthenticationWebFilter

AuthenticationWebFilter
具有以下依赖项:

…其中大多数默认情况下作为HttpBasic deps提供(从Spring Security源代码复制和粘贴):

private final ReactiveAuthenticationManagerResolver authenticationManagerResolver;
private ServerAuthenticationSuccessHandler authenticationSuccessHandler=新建WebFilterChainServerAuthenticationSuccessHandler();
私有服务器authenticationConverter authenticationConverter=新服务器HttpBasicAuthenticationConverter();
private ServerAuthenticationFailureHandler authenticationFailureHandler=新的ServerAuthenticationEntryPointFailureHandler(新的HttpBasicServerAuthenticationEntryPoint());
私有服务器securityContextRepository securityContextRepository=NoOpServerSecurityContextRepository.getInstance();//无状态会话
私有服务器WebExchangeMatcher requiresAuthenticationMatcher=ServerWebExchangeMatchers.anyExchange();
您可以使用
AuthenticationWebFilter
的setters方法设置任何需要的内容。
AuthenticationWebFilter
具有以下逻辑:


因此,根据具体情况,您必须配置一个或另一个依赖项。您可以在my repo中看到身份验证和授权工作原理的完整示例:(在kotlin中,但情况相同)

您将此bean放在何处?此bean是一个用@EnableWebFluxSecurity注释的secutryConfiguration类您是否将此注释-
@EnableReactiveMethodSecurity
放在配置类中?你如何检查你的经理从未被呼叫过?我正在调试和查看日志,我发现经理从未被呼叫过。我现在并不真正需要方法安全性——我不确定@EnableReactiveMethodSecurity会有什么帮助。你能详细说明一下吗?你把这个bean放在哪里?这个bean是一个用@EnableWebFluxSecurity注释的secutryConfiguration类。你把这个注释-
@EnableReactiveMethodSecurity
放在config类中了吗?你如何检查你的经理从未被呼叫过?我正在调试和查看日志,我发现经理从未被呼叫过。我现在并不真正需要方法安全性——我不确定@EnableReactiveMethodSecurity会有什么帮助。你能详细说明一下吗?谢谢!这很有帮助谢谢你!这很有帮助