-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Allow access to underlying WebSocketSession with WebSocketMessageBroker config [SPR-12314] #16919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Rossen Stoyanchev commented hey Rob, at the time of handshake there is no WebSocket session yet. It's just an HTTP request to initiate an upgrade. In that sense a HandshakeHandler merely facilitates establishing a session but never "sees" one. That said a HandshakeInterceptor can return false to prevent the handshake and thus the WebSocket session from getting established and it provides access to the request. Have you tried that? |
Rossen Stoyanchev commented
I realize you probably meant get the session at the time of handshake but close it later... doh! One option would be to decorate the WebSocketHandler and thus intercept the afterSessionEstablished callback. That's the first time the application (and the framework) gets a hold of the WebSocket session. I'll set this for 4.1.2 until we clear up there is a good way for Spring Security to do this. |
Rob Winch commented Rossen Stoyanchev Thanks for the response. Indeed I could have worded that a bit better. You are correct to assume that I do want to close the WebSocket Session later (i.e. when the HttpSession expires or is closed). I'm wondering if you meant WebSocketHandler#afterConnectionEstablished(WebSocketSession) since I do not see a I looked into this, but I had a few problems: Obtaining the HttpSession idI could not find an elegant way to access the
This solution doesn't seem that ideal because:
I can live with these problems, but I'm wondering if there is something I am missing. ConfigurationThe second problem was one I haven't gotten past yet. How do you decorate the existing WebSocketHandler? The documentation states how to configure a WebSocketHandler, but it does not appear to have instructions for getting this to work with |
Rossen Stoyanchev commented After a chat, Rob and I agreed to try the following:
Note the latter will require having a WebSocketHandlerDecoratorFactory. |
Rossen Stoyanchev commented Rob Winch, the two items from the last comment are now ready. Please give it a try. |
Rob Winch commented Rossen Stoyanchev I was able to get this to work well. One thing I am still debating is if Spring Session will use the HttpSessionhandshakeInterceptor or provide its own implementation. The reasons:
The take away is...it may be alright (from a Spring Session perspective) to remove the feature for saving the session id from HttpSessionhandshakeInterceptor. Honestly, I see this as something valuable for others, so feel free to leave it too. |
Rossen Stoyanchev commented Rob, that sounds fine of course. I'm just curious what you mean by "there is no way to prevent this short of specifying a Single Session Attribute that is unlikely". It should be possible to specify no attributes to be copied or is there an issue there? |
Rob Winch commented I did not try this out, so I could be wrong. However, looking at the source it appears that if CollectionUtils.isEmpty() returns true, then the attribute will be copied. CollectionUtils.isEmpty returns true if the collection is null or empty. So it appears that specifying no attributes means that all attributes are copied. |
Rossen Stoyanchev commented Good point. I made some improvements. You can now pass an empty collection of attributes and/or set a flag explicitly whether to copy all attributes or not (see the tests. |
Rob Winch commented Thanks! |
Rob Winch opened SPR-12314 and commented
It would be nice to provide a hook that allows intercepting the HttpSession id, HttpServletRequest#getUserPrincipal(), and a reference to be able to close the WebSocket Session at the time of the handshake.
This is necessary for things like Spring Session which want to provide a mechanism to override the HttpSession since JSR-356 states:
The closest mechanism I could find to do this would be to create a HandshakeHandler. However, there doesn't appear to be a reliable way to refer to an object that can close the WebSocket Session.
Issue Links:
Referenced from: commits 97596fb, f0323be
The text was updated successfully, but these errors were encountered: