如何访问“会话"Spring WebSocket 处理程序中的作用域 bean(不是“websocket"作用域) [英] How to access "session" scoped beans in a Spring WebSocket handler (not "websocket" scope)

查看:31
本文介绍了如何访问“会话"Spring WebSocket 处理程序中的作用域 bean(不是“websocket"作用域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在原始 Spring WebSocket 应用程序(不使用 sockjs/STOMP 或任何其他中间件)中,我如何让 Spring 注入已在 HTTP 会话范围中注册的 bean,以便它们可以被我的 中的代码使用WebSocketHandler bean?

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

请注意,我不是问的是以下任何一个问题:

Note that what I am not asking is any of these questions:

  • How do I create beans in a scope that is accessible to all handler invocations for the same WebSocket session (e.g. as described in the answer to Request or Session scope in Spring Websocket). The beans I need to access already exist in the scope for the HTTP session
  • How do I (programatically) access objects in the servlet container's HTTP session storage (I haven't tried to do this, but I'm pretty sure the answer involves using an HttpSessionHandshakeInterceptor), but that doesn't get me injection of Spring scoped dependencies.
  • How to use a ScopedProxy to pass beans between code in different scopes (e.g. as described here); I'm already familiar with how to do this, but attempting to do so for a WebSocketHandler causes an error because the session scope hasn't been bound to the thread at the point the object is accessed.
  • How to access the current security principal -- again, very useful, but not what I'm currently trying to achieve.

我希望做的是提供一个简单的框架,允许 MVC 应用程序的传统 HTTP 请求启动部分直接与 WebSocket 协议通信(用于向客户端发送简单的推送更新).我希望能够做的是将数据从 MVC 控制器推送到会话范围的对象中,然后在 websocket 处理程序中将其拉出.我想要最简单的 API,从 MVC 控制器的角度来看,如果可以为此只使用会话范围的 bean,那将是理想的.如果您对共享这些数据的非常简单的方法有任何其他想法,如果这种方法不可行,我也希望听到这些想法.

What I'm hoping to do is provide a simple framework that allows for the traditional HTTP-request initiated parts of an MVC application to communicate directly with a WebSocket protocol (for sending simple push updates to the client). What I want to be able to do is push data into a session scoped object from the MVC controller and pull it out in the websocket handler. I would like the simplest possible API for this from the MVC controller's perspective, which if it is possible to just use a session-scoped bean for this would be ideal. If you have any other ideas about very simple ways of sharing this data, I'd also like to hear those in case this approach isn't possible.

推荐答案

您也可以将 Java API 用于 websocket.此链接 https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support解释了如何用 Spring 做到这一点.不幸的是,这样的事情

You can also use Java API for websocket. This link https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support explains how to do this with Spring. Ufortunately, something like this

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

     private SessionScopedBean sessionScopedBean;

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

导致异常(因为我们试图在其范围之外访问 bean),但对于单例 bean 和原型 bean,它运行良好.

causes exception (because we're trying to access bean outside its scope), but for singleton and prototype beans it works well.

要使用会话属性,您可以修改 hanshake 并传递所需的属性:

To work with session attributes you can modify the hanshake and pass required attributes:

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"));
    }
}

P.S. 与其说是回答,不如说是评论.我只是想在您的问答中添加另一种处理 websocket 会话属性的方法.我一直在网上搜索完全相同的问题,上面显示的方式在我看来是在 websocket 中处理会话数据的最系统的方法.

P. S. More a comment than an answer. I just wanted to add another way of handling session attributes in websocket to your question-answer. I have been searching the web for exactly the same issue and the way showed above seems to me the most systematic approach to handling the session data in websocket.

这篇关于如何访问“会话"Spring WebSocket 处理程序中的作用域 bean(不是“websocket"作用域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆