繁体   English   中英

Spring Websocket具有安全性-每个用户都可以订阅任何其他用户队列?

[英]Spring websocket with stomp security - every user can subscribe to any other users queue?

我创建了一个使用Spring 4的websockets机制的简单应用程序。在我的应用程序中使用了activemq代理。

在我的简单测试中,我为名为“ Alejando”的用户(用户/ alejandro / queue / greetings)创建了10条消息

当我使用“ Alejando”登录并订阅该队列时:

  stompClient.subscribe('/user/alejandro/queue/greetings', function(greeting){
                  showGreeting(JSON.parse(greeting.body).content);
  }); 

实际上,我确实收到了有关alejandro的所有10条消息。

问题是,当我使用名为“ evilBart”的其他用户登录并订阅alejandro队列时,我也收到了消息吗?

我该如何加强安全性? 我希望用户只能订阅它自己的队列。

谢谢!

我的配置类:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableStompBrokerRelay("/queue/","/topic","/user/");     
    config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/hello").withSockJS();
}

}

检查这个类似的问题 :您必须使用Spring Security通过HTTP对用户进行身份验证,然后使用SimpMessageTemplate.convertAndSendToUser()方法将消息发送给用户。

您可以采取两种选择。

  1. 只需从config.enableStompBrokerRelay中删除“ / user /”。 Spring消息将自动添加前缀。

    convertAndSendToUser不用于代理中继。

参见org.springframework.messaging.simp.user软件包源码



默认用户前缀为“ / user /”。 您可以使用config.setUserDestinationPrefix()进行更改





2.重写两个方法并从ChannelInterceptor处理它

方法:

  • configureClientInboundChannel
  • configureClientOutboundChannel
  • 暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM