Spring-framework: Subscriber context not propagated to WebSocketHandler for Tomcat websockets

Created on 3 Dec 2020  路  4Comments  路  Source: spring-projects/spring-framework

In a WebFlux application that uses Tomcat as the webserver, the reactor subscriber context is not currently propagated from WebFilters down to WebSocketHandlers. Therefore objects put into the subscriber context (such as the spring security context) by WebFilters are not available to WebSocketHandlers.

For comparison, when reactor-netty is used as the webserver, the reactor subscriber context _is_ propagated from WebFilters to WebSocketHandlers properly.

I have a WebSocketHandler that needs to access the spring security context. Unfortunately, this is not currently possible when using tomcat as the webserver.

The disconnect occurs between TomcatRequestUpgradeStrategy and StandardWebSocketHandlerAdapter...

  1. TomcatRequestUpgradeStrategy.upgrade constructs a StandardWebSocketHandlerAdapter and delegates to the container's websocket upgrade logic (see here). The context is lost at this point.
  2. The container performs the websocket upgrade and calls into StandardWebSocketHandlerAdapter.onOpen.
  3. StandardWebSocketHandlerAdapter.onOpen then delegates to WebSocketHandler.handle (see here). I need access to items in the subscriber context within aWebSocketHandler, but the context is empty.

To propagate the context, I believe TomcatRequestUpgradeStrategy.upgrade could pass the active context to the StandardWebSocketHandlerAdapter upon construction (during step 1 above).
Then StandardWebSocketHandlerAdapter.onOpen could pass the context to the handler stream (during step 3 above).

web enhancement

Most helpful comment

Sure, let's see how it works out.

All 4 comments

Sounds reasonable, will give it a try for 5.3.2.

Thanks Rossen!

If it's simple, I'd love a backport to 5.2.x too if possible.

Sure, let's see how it works out.

It turned out slightly more involved. We can consider a backport but since 5.2.13 is not planned to be released at the same time, I'd prefer to defer for now and see first if 5.3.3 causes any issues.

Was this page helpful?
0 / 5 - 0 ratings