基于某些标头值跳过Spring流量安全性

c86crjj0  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(175)

我的要求完全不同,实际上我已经在spring cloud api gateway(基于webflux)中添加了安全性,现在基本上我想在端点/testapi/上存在一些头值的基础上跳过oauth资源服务器完整流,否则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安全的新手。

p4tfgftt

p4tfgftt1#

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

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

    }

并在securityconfig中进行了相同的配置

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

相关问题