How to access beans session area in WebSocket Spring handler (and not in websocket area)

In a raw Spring WebSocket application (without using sockjs / STOMP or any other middleware), how can I insert beans that have been registered in the HTTP session scope so that they can be used in the code in my WebSocketHandlerbean?

Please note that I am not asking any of these questions:

  • How to create beans in an area accessible to all handler-handlers for the same WebSocket session (for example, as described in the response of the request area or communication session in Spring Websocket ). beans I need to access already in scope for an HTTP session
  • How can I (programmatically) access the objects in the session store in the servlet container (I have not tried to do this, but I am sure that the answer involves using the HttpSessionHandshakeInterceptor ), but this does not give me injection of Spring dependent regions.
  • How to use ScopedProxy to transfer beans between code in different areas (for example, as described here ); I already know how to do this, but trying to do this for an WebSocketHandlererror, because the area is sessionnot bound to a thread at the access point to the object.
  • How to access the current security section is, again, very useful, but not what I'm currently trying to achieve.

, , , MVC, HTTP-, WebSocket ( push- ). , , - MVC websocket. API MVC, , bean , . , , .

+4
1

Java API websocket. https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support , Spring. , -

@ServerEndpoint(value = "/sample", configurator = SpringConfigurator.class)
public class SampleEndpoint {

     private SessionScopedBean sessionScopedBean;

     @Autowired
      public SampleEndpoint(SessionScopedBean sessionScopedBean) {
        this.sessionScopedBean = sessionScopedBean;
      }
}

( bean ), singleton prototype beans .

hanshake :

public class CustomWebSocketConfigurator extends SpringConfigurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig config,
            HandshakeRequest request,
            HandshakeResponse response) {

        //put attributes from http session to websocket session
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        config.getUserProperties().put("some_attribute",
               httpSession.getAttribute("some_attribute_in_http_session"));
    }
}

. S. , . websocket -. ​​ , , , websocket.

+1

Source: https://habr.com/ru/post/1664661/


All Articles