How to access request object in @MessageMapping method with spring websocket implementation

百般思念 提交于 2019-12-08 07:21:23

问题


I am integrating an existing spring MVC web application with spring websockets. I was successfully able to integrate by following the instructions in

https://spring.io/guides/gs/messaging-stomp-websocket/

The existing web application has a filter, which sets a few of the attributes. I have a requirement to access the attributes set by the filter in the controller i,e in @MessageMapping method.

Could some one tel how can we access the request object in the @MessageMapping method?


回答1:


When a STOMP client connects to the application, it first has to request a protocol upgrade to switch to websocket. Once using that websocket connection, the messages sent/received don't go through your regular Servlet filter - only the first HTTP request (the "Handshake") did.

Depending on your use case, there are several ways to achieve this.

If it's related to Authentication, then there are existing features for this in the Spring Framework, but also in Spring Security.

If it's related to the HTTP session, you can easily ask for all HTTP session attributes to be copied into the websocket session - or even customize the Handshake for your own needs (see reference doc). Once done, you can inject the Websocket scope in a @MessageMapping controller method and get those attributes (see reference doc).



来源:https://stackoverflow.com/questions/29215393/how-to-access-request-object-in-messagemapping-method-with-spring-websocket-imp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!