Tag: stomp

保护基于Spring消息传递的websocket服务

我现在正在研究这个问题已有3个星期没有真正的解决方案,我真的希望你能帮助我。 一点项目背景: 使用基于JavaScript / PHP的客户端的Webapp通过SocksJS发送消息并向“门”发送消息 门是用Java / Spring编写的,使用@SendTo和@MessageMapping来发送和接收消息 来自门的消息被发送到RabbitMQ并通过“messageBrokerRegistry.enableStompBrokerRelay”返回给客户端 到目前为止它的工作原理,发送的消息都会回来。 现在是高级安全部分: 消息应该通过包含用户和东西的cookie来保护…… 据我所知,WebSockets本身并不支持安全性。 您必须像使用BASIC auth或类似的“常用”webapp一样保护您的webapp。 所以我添加了一个servletfilter,其中包含一个扩展GenericFilterBean的类。 如果用户发送了正确的cookie页面加载,否则他会收到403错误。 现在出现了问题: 由于@SendTo向所有订阅者发送消息而@SendToUser似乎只将其发送到一个会话,因此我倾向于使用@SendToUser。 但似乎无法选择要创建的rabbitMQ队列。 我想要一些像“/ myqueue-user-123”。 @SendToUser无法做到这一点,因为生成的队列是随机的,并且基于SessionID,我无法覆盖。 所以我尝试过(我尝试除了拦截器,事件等之外的其他东西),使用@SendTo没有值,以便客户端可以决定它必须发送到的队列。 我现在需要的是评估cookie中的用户与“/ myqueue / user-123”相关联。 如果不是,请不要向他发送消息。 阻止他订阅。 断开他,无论如何。 但在我看来,你绝对不能 – 阻止发送消息,只是“拦截”以记录它们而不是改变 – 断开websocket因为它自动尝试重新连接 – 抛出exception,因为订阅仍然继续(事件只是事件,不是要干涉的东西)。 我会非常感谢任何建议或提示。 因为我完全被困在这里……

Spring 4 STOMP Websockets Heartbeat

我似乎找不到如何在Spring中使用websockets向客户端发送心跳的好资源! 我有一个使用此配置运行的基本服务器: @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker(“/room”); config.setApplicationDestinationPrefixes(“/app”); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint(“/channels”).withSockJS(); } } 然后我使用这样的东西向订阅房间的人发送消息: this.simpMessagingTemplate.convertAndSend(“/room/” + this.roomId, message); 这是用于与服务器通信的客户端代码: this.connect = function (roomNameParam, connectionCallback) { var socket = new SockJS(‘http://localhost:8080/channels’), self.stompClient = Stomp.over(socket); self.stompClient.connect({}, function (frame) { self.stompClient.subscribe(‘/room/’ + roomNameParam, […]

如何使用Spring 4在我的webSocket服务器中捕获订阅事件

我在spring https://spring.io/guides/gs/messaging-stomp-websocket/之后用spring 4,STOMP和sock.js做了简单的web socket通信。 现在我想将其升级为简单聊天。 我的问题是,当用户订阅新的聊天室时,他应该过去的消息。 我不知道如何捕捉他订阅时向他发送消息列表的那一刻。 我尝试使用@MessageMapping注释,但没有取得任何成功: @Controller public class WebSocketController { @Autowired private SimpMessagingTemplate messagingTemplate; @MessageMapping(“/chat/{chatId}”) public void chat(ChatMessage message, @DestinationVariable String chatId) { messagingTemplate.convertAndSend(“/chat/” + chatId, new ChatMessage(“message: ” + message.getText())); } @SubscribeMapping(“/chat”) public void chatInit() { System.out.println(“worked”); int chatId = 1; //for example messagingTemplate.convertAndSend(“/chat/” + chatId, new ChatMessage(“connected”)); } } 然后我创建了: […]

Spring STOMP从应用程序的任何位置发送消息

我正在构建一个使用Stomp通过websockets代理消息的应用程序。 我试图从服务器发送消息到客户端,而无需来自应用程序的任何地方的请求。 我在网上找到两个单独的选择,用于从应用程序的任何位置发送消息 第一个是在Websocket文档中找到的。 第20.4.5节: @Controller public class GreetingController { private SimpMessagingTemplate template; @Autowired public GreetingController(SimpMessagingTemplate template) { this.template = template; } @RequestMapping(value=”/greetings”, method=POST) public void greet(String greeting) { String text = “[” + getTimestamp() + “]:” + greeting; this.template.convertAndSend(“/topic/greetings”, text); } } 第二个是由一位Spring Blogger撰写的指南 : @Controller public class GreetingController { @Autowired private SimpMessagingTemplate template; […]

在spring boot websocket中向特定用户发送通知

我想向特定客户发送通知。 例如用户名用户 @Configuration @EnableWebSocketMessageBroker public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { stompEndpointRegistry.addEndpoint(“/socket”) .setAllowedOrigins(“*”) .withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker(“/topic”, “/queue”); registry.setApplicationDestinationPrefixes(“/app”); } 调节器 @GetMapping(“/notify”) public String getNotification(Principal principal) { String username = “user”; notifications.increment(); logger.info(“counter” + notifications.getCount() + “” + principal.getName()); // logger.info(“usersend:”+sha.getUser().getName()) ; //user template.convertAndSendToUser(principal.getName(), “queue/notification”, […]

Spring 4中的websockets的动态消息映射

我想用spring新的websocket / stomp支持开发一个小聊天。 我想我不能用这样的东西: @MessageMapping(“/connect/{roomId}”) @SendTo(“/topic/newMessage”) public String connectToChatRoom(@PathVariable String roomId, Principal p) { return getTimestamp() + ” ” + p.getName() + ” connected to the room.”; } 这里有什么动态映射选项? 作为客户,我只想订阅我所在的房间。 提前致谢!

Spring session + Spring web socket。 根据会话ID将消息发送到特定客户端

我已经从堆栈溢出跟踪Quetion1和Quetion2 ,根据其sessionId向特定客户端发送消息,但无法找到成功。 下面是我的示例RestController类 @RestController public class SpringSessionTestApi { @Autowired public SimpMessageSendingOperations messagingTemplate; @MessageMapping(“/messages”) public void greeting(HelloMessage message, SimpMessageHeaderAccessor headerAccessor) throws Exception { String sessionId = (String) headerAccessor.getSessionAttributes().get(“SPRING.SESSION.ID”); messagingTemplate.convertAndSendToUser(sessionId,”/queue/test”,message, createHeaders(sessionId)); } private MessageHeaders createHeaders(String sessionId) { SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); headerAccessor.setSessionId(sessionId); headerAccessor.setLeaveMutable(true); return headerAccessor.getMessageHeaders(); } } 会话ID:当客户端发送createSession请求时,会生成新的spring sessionId,同样也会存储在MongoDB中。 在此之后,当客户端发送Web套接字连接请求时,会收到相同的sessionId,它按预期存储在mongoDb中。 直到这一切都运转良好。 现在我的工作是根据sessionId将响应发送回客户端。 为此我在web套接字类下面: @Configuration @EnableScheduling @EnableWebSocketMessageBroker public […]

Stomp spring web socket消息超出了大小限制

我正在我们的spring mvc web应用程序中实现spring web-socket。 但是当我尝试向端点发送一个非常大的消息时,我遇到了超过大小限制的消息。 我收到以下错误, message:The ‘content-length’ header 68718 exceeds the configured message buffer size limit 65536 14:49:11,506 ERROR [org.springframework.web.socket.messaging.StompSubProtocolHandler] (http-localhost/127.0.0.1:8080-4) Failed to parse TextMessage payload=[13684590},..], byteCount=16384, last=true] in session vlsxdeol. Sending STOMP ERROR to client.: org.springframework.messaging.simp.stomp.StompConversionException: The ‘content-length’ header 68718 exceeds the configured message buffer size limit 65536 at org.springframework.messaging.simp.stomp.BufferingStompDecoder.checkBufferLimits(BufferingStompDecoder.java:148) [spring-messaging-4.1.6.RELEASE.jar:4.1.6.RELEASE] at org.springframework.messaging.simp.stomp.BufferingStompDecoder.decode(BufferingStompDecoder.java:124) […]

使用sockjs使用Spring 4 WebSocket无法连接套接字

尝试使用sockjs在套接字上使用带有STOMP的Spring 4 WebSocket。 我遇到了一个问题。 我的配置: websocket.xml – spring上下文的一部分 控制器代码: @MessageMapping(“/ws”) @SendTo(“/topic/ws”) public AjaxResponse hello() throws Exception { AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setSuccess(true); ajaxResponse.addSuccessMessage(“WEB SOCKET!!! HELL YEAH!”); return ajaxResponse; } 客户端: var socket = new SockJS(“”); var stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { alert(‘Connected: ‘ + frame); stompClient.send(“/app/ws”, {}, {}); stompClient.subscribe(‘/topic/ws’, function(response){ alert(response.success); }); }); […]

如何在连接到spring websocket时向用户发送消息

我想在连接到spring websocket时向用户发送消息,我已经 @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Autowired private GenervicSerice userService; @Autowired private SimpMessagingTemplate template; private CurrentUser currnetUser; @Override public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { // TODO Auto-generated method stub stompEndpointRegistry.addEndpoint(“/ws”).withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker(“/queue/”, “/topic/”, “/exchange/”); config.setApplicationDestinationPrefixes(“/app”); } @Override public void configureClientInboundChannel(ChannelRegistration registration) { registration.setInterceptors(myChannelInterception()); try { updateNotificationAndBroadcast(); […]