Webfilter not getting triggered on each request in a spring reactive applicaiton (kotlin)

40 Views Asked by At

I am trying to set header values in the context and then use it inside other parts of application -

@Component
class CustomWebFilter : WebFilter {
    override fun filter(
        exchange: ServerWebExchange,
        chain: WebFilterChain,
    ): Mono<Void> {
        println("getting triggered")
        val tenant: String = exchange.request.headers["tenant-header"]?.get(0) ?: "default"
        return chain.filter(exchange).contextWrite { context ->
            var updatedContext = context.put("tenant", tenant)
            updatedContext
        }
    }

    
}

and trying to read "some_key" from context like this -

fun getContextValueReactive(): Mono<String> {
    return Mono.deferContextual { ctx ->
        val value = ctx.getOrDefault("tenant", "")
        value.toMono()
    }
}

Unfortunately the filter is not triggering on each http request and not setting the context, it remains empty

I am using springboot 3.1.1

What am I missing here? Shall I use any alternative way to achieve this

tried a few approaches from internet, didn't helped. Like putting @Bean and correct componentscan

0

There are 0 best solutions below