In the classic spring-mvc it is possible to set request scoped attributes on a RequestContextHolder
. Building on that, we can parse an incoming request in a HandlerInterceptorAdapter
, set request parameters such as currently logged in user, unique request ID (for log correlation) and so on.
These request attributes can be retrieved statically from any service (not only controllers) during the request's lifetime.
I am trying to achieve something similar with spring-webflux.
I could use a WebFilter
to intercept all incoming requests, get the current ServerWebExchange
and set attributes on it. However I don't see any way to get the current request's ServerWebExchange
anywhere else other than controller methods.
I am looking for a better solution than passing ServerWebExchange
(or ServerHttpRequest
) all around.
It seems like this is difficult to achieve in webflux since we cannot rely on saving variables associated with a particular request on ThreadLocal (because of the non-blocking architecture, a single thread can switch between requests mid-flight).
Still, this is an important requirement. Maybe there is a different approach?