2016-09-22 50 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