【问题标题】:Skip Spring Flux Security on the basis of some header values基于某些标头值跳过 Spring Flux Security
【发布时间】:2021-09-15 16:18:20
【问题描述】:

我的要求完全不同,实际上我已经在 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 安全性。

【问题讨论】:

    标签: spring-boot spring-security oauth-2.0 spring-webflux


    【解决方案1】:

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

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

    在 SecurityConfig 中配置相同

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

    【讨论】:

      猜你喜欢
      • 2017-04-28
      • 2019-11-15
      • 1970-01-01
      • 2019-11-12
      • 2022-09-23
      • 2016-09-10
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      相关资源
      最近更新 更多