Vib*_*pal 2 spring-security spring-webflux
我正在尝试为我的 spring-webflux 应用程序构建自定义身份验证管理器。然而我发现我的经理从来没有被打电话过。我的代码如下:
@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
return http
.authorizeExchange().pathMatchers("/**").authenticated().and().httpBasic().disable()
.securityContextRepository(webSessionServerSecurityContextRepository())
.addFilterAfter(new AuthenticationWebFilter(bearerTokenAuthenticationManager()),
SecurityWebFiltersOrder.REACTOR_CONTEXT)
.build();
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
假设你把这个bean放在一个带有注释的类中@Configuration,@EnableWebFluxSecurity你的问题似乎是你没有禁用csrfSpring Security默认配置的。
您可以通过以下方式做到这一点:
@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();
}
Run Code Online (Sandbox Code Playgroud)
此外,您必须正确配置AuthenticationWebFilter.
AnAuthenticationWebFilter具有以下依赖关系:
...其中大多数默认作为 HttpBasic deps 提供(从 Spring Security 源代码复制并粘贴):
private final ReactiveAuthenticationManagerResolver<ServerWebExchange> authenticationManagerResolver;
private ServerAuthenticationSuccessHandler authenticationSuccessHandler = new WebFilterChainServerAuthenticationSuccessHandler();
private ServerAuthenticationConverter authenticationConverter = new ServerHttpBasicAuthenticationConverter();
private ServerAuthenticationFailureHandler authenticationFailureHandler = new ServerAuthenticationEntryPointFailureHandler(new HttpBasicServerAuthenticationEntryPoint());
private ServerSecurityContextRepository securityContextRepository = NoOpServerSecurityContextRepository.getInstance(); // Stateless session
private ServerWebExchangeMatcher requiresAuthenticationMatcher = ServerWebExchangeMatchers.anyExchange();
Run Code Online (Sandbox Code Playgroud)
您可以使用 的 setters 方法设置您想要的任何内容AuthenticationWebFilter。AnAuthenticationWebFilter的逻辑如下:
因此,根据情况,您必须配置一个依赖项或另一个依赖项。您可以在我的存储库中看到身份验证和授权如何工作的完整示例: https: //github.com/soasada/kotlin-coroutines-webflux-security(在 kotlin 中,但情况相同)
| 归档时间: |
|
| 查看次数: |
2155 次 |
| 最近记录: |