具有spring安全性和webflux公共模式的过滤器链
我可能错过了一些明显的东西,但我自己却无法理解。所以我希望你能帮助我 我正在尝试保护SpringWebFlux应用程序,实际上只是应用程序的一个子集,我希望一些端点不受保护。这是我的安全配置:具有spring安全性和webflux公共模式的过滤器链,spring,security,spring-webflux,Spring,Security,Spring Webflux,我可能错过了一些明显的东西,但我自己却无法理解。所以我希望你能帮助我 我正在尝试保护SpringWebFlux应用程序,实际上只是应用程序的一个子集,我希望一些端点不受保护。这是我的安全配置: @EnableReactiveMethodSecurity @EnableWebFluxSecurity public class WebSecurityConfig { ... @Bean public SecurityWebFilterChain springSecuri
@EnableReactiveMethodSecurity
@EnableWebFluxSecurity
public class WebSecurityConfig {
...
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.pathMatchers("/process_login", "/login", "/logout").permitAll()
.pathMatchers("/api/team").permitAll()
.anyExchange().authenticated()
.and()
.addFilterAt(webFilter(), SecurityWebFiltersOrder.AUTHORIZATION)
.addFilterAt(new AuthorizationModifierFilter(),SecurityWebFiltersOrder.AUTHENTICATION)
;
http.httpBasic().disable()
.formLogin().disable()
.logout().disable();
return http.build();
}
...
}
当访问/api/team端点时,我希望不会通过安全过滤器链,但我正在通过我的安全过滤器
知道我做错了什么吗
谢谢你的帮助
马克不要打破链条。应该是这样的。试试这个,如果它不工作,我会相应地更新
private static String[] permittedUrl = new String[]{
"/process_login", "/login", "/logout", "/api/team"
};
........
........
return http
.csrf().disable()
.cors().disable()
.formLogin().disable()
.httpBasic().disable()
.exceptionHandling()
.authenticationEntryPoint((swe, e) ->
Mono.fromRunnable(() -> {
swe.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
})
)
.accessDeniedHandler((swe, e) ->
Mono.fromRunnable(() -> {
swe.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
})
)
.and()
.authorizeExchange()
.pathMatchers(permittedUrl).permitAll()
.anyExchange()
.authenticated()
.and()
.build();
嗨,Shoshi你的解决方案帮助我发现我看错了方向。你是对的,断链似乎并不理想,你的方法帮助我确定我是手动将自己的ServerWebExchangeMatcher注入到安全链中的,但正如我将其声明为bean一样,我认为Spring正在所有链中注入它。所以我需要一些时间来检查我的完整配置。我很快会回来更新信息;要么是我的工作状态,要么是另一个问题:)太好了。希望你能找到解决办法。让我知道。好的,开始工作了。我正在注入的webfilter始终被调用(无论请求是否在permitAll()列表中)。因此,我将这个列表注入到我的webFilter中,并使用一个否定的ServerWebExchangeMatcher作为authenticationMatcher。现在,它的工作如预期。