繁体   English   中英

根据某些标头值跳过 Spring Flux Security

[英]Skip Spring Flux Security on the basis of some header values

我的要求完全不同,实际上我已经在 Spring Cloud API Gateway(基于 WebFlux)中添加了安全性,现在基本上我想跳过 OAuth 资源服务器的完整流程,如果端点 /testAPI/** 上存在某些标头值,否则OAuth 资源服务器流将在相同的端点 /testAPI/** 上启动

安全配置

    @Autowired
    private ReactiveAuthenticationManager manager;
    

    @Bean
    SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        return http
                .authorizeExchange()
                
                .pathMatchers("/testAPI/**").authenticated()
                .anyExchange().permitAll()
                .and()
                
                .securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
                
                .httpBasic().disable()
                .formLogin().disable()
                .csrf().disable()
                .logout().disable()
                .oauth2ResourceServer()
                    .jwt()
                    .authenticationManager(manager)
                .and()
                .and()
                
                .addFilterBefore(new SecurityWebFilter(), SecurityWebFiltersOrder.AUTHENTICATION)
                
                .build();

在 SecurityWebFilter 中,我尝试将 ReactiveSecurityContextHolder.withAuthentication(..) 与自定义身份验证设置为 true 并使用

Mono<Authentication> authentication = ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication)
                .doOnSuccess(auth -> {
                    auth.setAuthenticated(true);
                });

没有什么对我有用,我可以使用 OAuth 服务器进行验证,但我想在相同的端点上有条件地跳过它。

请帮忙,我是 WebFlux 的新手,正在探索 Spring 安全性。

我创建了一个自定义匹配器

private ServerWebExchangeMatcher authorizationHeaderMatcher() {
        return (exchange) -> exchange.getRequest().getHeaders().containsKey(HttpHeaders.AUTHORIZATION)
                ? MatchResult.match()
                : MatchResult.notMatch();

    }

并在 SecurityConfig 中配置相同

.securityMatcher(customAuthorizationMatcher())
                .authorizeExchange()
                .pathMatchers("/testAPI/**").authenticated()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM