2016-09-22 51 views
1

我有一个接受SUBSCRIBE请求的Spring Websocket Stomp应用程序。Spring websocket STOMP退订eventHandler

在应用我有一个订阅处理,也就是

@Component 
public class SubscribeStompEventHandler implements ApplicationListener<SessionSubscribeEvent> { 

    @Override 
    public void onApplicationEvent(SessionSubscribeEvent event) {} 
} 

,我用它来验证订阅。

如果订阅无效,例如,当前用户看不到该订阅,我希望Broker(我使用SimpleMessagingBroker)“忘记”该订阅,或者最好根本不注册它。

我的问题是:

  • 我可以做经纪人不登记认购,如果我将处理订阅请求的传入消息拦截和阻止消息的传播?

  • 该事件处理程序还可以使用什么来取消订阅?

+0

http://stackoverflow.com/questions/21554230/how-to-reject-topic-subscription-based-on-user-rights -with-spring-websocket – jahra

回答

1

您需要为您创建ChannelInterceptor实施。只需延长ChannelInterceptorAdapter并覆盖preSend(Message<?> message, MessageChannel channel)即可。在这里,您将可以访问标题和会话信息进行验证。你也需要registrate你的拦截器

@Override 
public void configureMessageBroker(MessageBrokerRegistry registry) { 
    registry.configureBrokerChannel().setInterceptors(new YourInterceptor()) 
    registry.enableSimpleBroker("/queue/", "/topic/"); 
    registry.setApplicationDestinationPrefixes("/app"); 
} 

更多的信息在这里How to reject topic subscription based on user rights with Spring-websocket

+0

看起来不错。你知道我是否从preSend返回null并且不抛出,它会有相同的效果吗? –

+0

你可以看看'AbstractMessageChannel.ChannelInterceptorChain.applyPreSend'方法。如果没有消息来自拦截器,它只会将标志设置为false。你最好试试它,因为我不知道会发生什么。@ AskarIbragimov – jahra