How to get the context of the current request in spring-webflux

Doron Gold

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?

Brian Clozel

The approaches you're describing are the ones currently supported. As you've underlined, using a static approach with ThreadLocals is not possible.

Reactor is looking into alternatives with a new context feature (see this PR). Spring is likely to pick that up and use it, but not necessarily for request attributes since the current model fits quite well.

If you'd like a particular extension point to intercept requests, please create a JIRA issue on the Spring Framework project, describing what you're trying to achieve and where things are failing.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spring webflux error handler: How to get the reactor context of the request in the error handler?

分類Dev

Webflux, How to intercept a request and add a new header

分類Dev

Spring MVC: How to get context param when instantiate the bean?

分類Dev

How to set a timeout in Spring 5 WebFlux WebClient

分類Dev

How to set a timeout in Spring 5 WebFlux WebClient

分類Dev

How to use WebSession in Spring WebFlux to persist data?

分類Dev

Get current owin context in self host mode

分類Dev

Get current user with spring aop

分類Dev

Spring WebFlux WebClient-400 Bad Requestを解決する方法

分類Dev

Spring WebFlux/ Reactor core

分類Dev

Capturing response in spring Webflux

分類Dev

Spring WebFlux with Kafka and Websockets

分類Dev

Spring webflux testing

分類Dev

How to get the hostname of the request?

分類Dev

Spring @Async vs Spring WebFlux

分類Dev

How to get current seed in R

分類Dev

How to get the current ProcessID in UWP?

分類Dev

Swagger: Spring MVC models in GET request

分類Dev

Spring Framework WebFlux Reactive Programming

分類Dev

Spring WebFlux webclient handle ConnectTimeoutException

分類Dev

Java Spring WebFlux vs RxJava

分類Dev

How to get custom context into haystack index template

分類Dev

How to get array in http request

分類Dev

How to get distance between current location and the next current location

分類Dev

Spring Boot - How to kill current Spring Security session?

分類Dev

How to add error response format in Swagger for a Spring-boot project based on webflux

分類Dev

Spring WebFlux & Mockito: How to verify the stream was consumed and participating handlers were invoked

分類Dev

Spring Security - get current user fields from database

分類Dev

How to get current name of route in Vue?

Related 関連記事

  1. 1

    Spring webflux error handler: How to get the reactor context of the request in the error handler?

  2. 2

    Webflux, How to intercept a request and add a new header

  3. 3

    Spring MVC: How to get context param when instantiate the bean?

  4. 4

    How to set a timeout in Spring 5 WebFlux WebClient

  5. 5

    How to set a timeout in Spring 5 WebFlux WebClient

  6. 6

    How to use WebSession in Spring WebFlux to persist data?

  7. 7

    Get current owin context in self host mode

  8. 8

    Get current user with spring aop

  9. 9

    Spring WebFlux WebClient-400 Bad Requestを解決する方法

  10. 10

    Spring WebFlux/ Reactor core

  11. 11

    Capturing response in spring Webflux

  12. 12

    Spring WebFlux with Kafka and Websockets

  13. 13

    Spring webflux testing

  14. 14

    How to get the hostname of the request?

  15. 15

    Spring @Async vs Spring WebFlux

  16. 16

    How to get current seed in R

  17. 17

    How to get the current ProcessID in UWP?

  18. 18

    Swagger: Spring MVC models in GET request

  19. 19

    Spring Framework WebFlux Reactive Programming

  20. 20

    Spring WebFlux webclient handle ConnectTimeoutException

  21. 21

    Java Spring WebFlux vs RxJava

  22. 22

    How to get custom context into haystack index template

  23. 23

    How to get array in http request

  24. 24

    How to get distance between current location and the next current location

  25. 25

    Spring Boot - How to kill current Spring Security session?

  26. 26

    How to add error response format in Swagger for a Spring-boot project based on webflux

  27. 27

    Spring WebFlux & Mockito: How to verify the stream was consumed and participating handlers were invoked

  28. 28

    Spring Security - get current user fields from database

  29. 29

    How to get current name of route in Vue?

ホットタグ

アーカイブ