1

我有一个 webflux 应用程序,需要在我的身份验证日志中包含 sleuth 上下文(从 DefaultWebFilterChain 移动到 SecurityWebFilterChain)。

我试图在我的安全链中手动添加:

public GwApiSecurityConfig(Tracer tracer, HttpServerHandler httpServerHandler, CurrentTraceContext currentTraceContext){
    this.tracer = tracer;
    this.httpServerHandler = httpServerHandler;
    this.currentTraceContext = currentTraceContext;
}

@Bean
public SecurityWebFilterChain securityFilterChain(ServerHttpSecurity http) {
    return http
            .csrf().disable()
            .authorizeExchange()
            .pathMatchers(HttpMethod.GET,"/swagger", "/v3/**", "/webjars/**", "/actuator/**").permitAll()
            .anyExchange().authenticated()
            .and()
            .httpBasic()
            .authenticationEntryPoint((exchange, exception) -> Mono.error(new GwException(HttpStatus.UNAUTHORIZED, GwError.AUTHENTICATION)))
            .and()
            .formLogin().disable()
            .redirectToHttps()
            .and()
            .addFilterBefore(new TraceWebFilter(tracer, httpServerHandler, currentTraceContext), SecurityWebFiltersOrder.HTTP_BASIC)
            .build();
}

但我得到一个错误:

    09:11:06.117 ERROR[reactor-http-nio-2] [,] GwApiErrorHandler - null
java.lang.NullPointerException: null
    at org.springframework.cloud.sleuth.instrument.web.TraceWebFilter.spanFromContextRetriever(TraceWebFilter.java:139)

我还检查了这个属性:

spring.sleuth.web.filter-order=1

但我认为只会影响 DefaultWebFilterChain 的顺序,不会影响 SecurityWebFilterChain。以及如何从 DefaultWebFilterChain 中删除过滤器以避免过滤两次?

有任何想法吗?谢谢!

4

0 回答 0