18

I have added custom token based authentication for my spring-web app and extending the same for spring websocket as shown below

public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
    }

    @Override
      public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.setInterceptors(new ChannelInterceptorAdapter() {

            @Override
            public Message<?> preSend(Message<?> message, MessageChannel channel) {

                StompHeaderAccessor accessor =
                    MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);

                if (StompCommand.CONNECT.equals(accessor.getCommand())) {
                    String jwtToken = accessor.getFirstNativeHeader("Auth-Token");
                        if (!StringUtils.isEmpty(jwtToken)) {
                            Authentication auth = tokenService.retrieveUserAuthToken(jwtToken);
                            SecurityContextHolder.getContext().setAuthentication(auth);
                            accessor.setUser(auth);
                            //for Auth-Token '12345token' the user name is 'user1' as auth.getName() returns 'user1'
                        }
                }

                return message;
            }
        });
      }
}

The client side code to connect to the socket is

var socket = new SockJS('http://localhost:8080/gs-guide-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({'Auth-Token': '12345token'}, function (frame) {
        stompClient.subscribe('/user/queue/greetings', function (greeting) {
            alert(greeting.body);
        });
    });

And from my controller I am sending message as

messagingTemplate.convertAndSendToUser("user1", "/queue/greetings", "Hi User1");

For the auth token 12345token the user name is user1. But when I send a message to user1, its not received at the client end. Is there anything I am missing with this?

BiJ
  • 1,639
  • 5
  • 24
  • 55

2 Answers2

30

In your Websocket controller you should do something like this :

@Controller
public class GreetingController {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @MessageMapping("/hello")
    public void greeting(Principal principal, HelloMessage message) throws  Exception {
        Greeting greeting = new Greeting();
        greeting.setContent("Hello!");
        messagingTemplate.convertAndSendToUser(message.getToUser(), "/queue/reply", greeting);
    }
}

On the client side, your user should subscribe to topic /user/queue/reply.

You must also add some destination prefixes :

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

When your server receive a message on the /app/hello queue, it should send a message to the user in your dto. User must be equal to the user's principal.

I think the only problem in your code is that your "/user" is not in your destination prefixes. Your greetings messages are blocked because you sent them in a queue that begin with /user and this prefixe is not registered.

You can check the sources at git repo : https://github.com/simvetanylen/test-spring-websocket

Hope it works!

Oreste Viron
  • 3,592
  • 3
  • 22
  • 34
  • 1
    See the thing is, I want the message to be sent to only a specific user. If I use `@SendTo("/user/queue/greetings")`, how would the application know to which user I want to send the message to. Also I tried what you suggested but didn't receive any notification at the client side. – BiJ Apr 05 '17 at 12:26
  • I have already done this. I will try to find my code and post it here. – Oreste Viron Apr 05 '17 at 12:51
  • It will be a great help. – BiJ Apr 05 '17 at 14:44
  • Thanks a lot. It helped a lot. Things were not working for me as I was using spring boot 1.5.2.RELEASE and version 0.0.1-SNAPSHOT. – BiJ Apr 07 '17 at 05:47
  • Don't you think you are exposing `subscriber/user` in your `function sendName()` [here](https://github.com/simvetanylen/test-spring-websocket/blob/master/src/main/resources/static/app.js). Anyone can guess `Subscriber` e.g. `John, Harry ... etc` – Shantaram Tupe Dec 02 '17 at 11:36
  • Thanks a lot!! you saved my day.!! config.setUserDestinationPrefix("/user"); worked , I was missing this! – voucher_wolves Jul 24 '18 at 05:43
  • why every one relate websocket with controller? isn't bidirectional ? why we wait a message from the client ? – maroodb Jan 20 '20 at 22:14
  • @OresteViron why do we have to add /user in enableSimpleBroker ? It only worked if I add it over there. – Suraj Gautam Aug 19 '20 at 15:46
6

In my previous project I sent messages to one specific user; in detail I wrote the following:

CLIENT SIDE:

function stompConnect(notificationTmpl) 
{
    var socket = new SockJS('/comm-svr');
    stompClient = Stomp.over(socket);
    var theUserId 
    stompClient.connect({userId:theUserId}, function (frame) {
            debug('Connected: ' + frame);
            stompClient.subscribe('/topic/connect/'+theUserId, function (data)                   {
//Handle data
              } 
        });
}

SERVER SIDE

Spring websocket listener:

@Component
public class WebSocketSessionListener
{
    private static final Logger logger = LoggerFactory.getLogger(WebSocketSessionListener.class.getName());
    private List<String> connectedClientId = new ArrayList<String>();

    @EventListener
    public void connectionEstablished(SessionConnectedEvent sce)
    {
        MessageHeaders msgHeaders = sce.getMessage().getHeaders();
        Principal princ = (Principal) msgHeaders.get("simpUser");
        StompHeaderAccessor sha = StompHeaderAccessor.wrap(sce.getMessage());
        List<String> nativeHeaders = sha.getNativeHeader("userId");
        if( nativeHeaders != null )
        {
            String userId = nativeHeaders.get(0);
            connectedClientId.add(userId);
            if( logger.isDebugEnabled() )
            {
                logger.debug("Connessione websocket stabilita. ID Utente "+userId);
            }
        }
        else
        {
            String userId = princ.getName();
            connectedClientId.add(userId);
            if( logger.isDebugEnabled() )
            {
                logger.debug("Connessione websocket stabilita. ID Utente "+userId);
            }
        }
    }

    @EventListener
    public void webSockectDisconnect(SessionDisconnectEvent sde)
    {
        MessageHeaders msgHeaders = sde.getMessage().getHeaders();
        Principal princ = (Principal) msgHeaders.get("simpUser");
        StompHeaderAccessor sha = StompHeaderAccessor.wrap(sde.getMessage());
        List<String> nativeHeaders = sha.getNativeHeader("userId");
        if( nativeHeaders != null )
        {
            String userId = nativeHeaders.get(0);
            connectedClientId.remove(userId);
            if( logger.isDebugEnabled() )
            {
                logger.debug("Connessione websocket stabilita. ID Utente "+userId);
            }
        }
        else
        {
            String userId = princ.getName();
            connectedClientId.remove(userId);
            if( logger.isDebugEnabled() )
            {
                logger.debug("Connessione websocket stabilita. ID Utente "+userId);
            }
        }
    }

    public List<String> getConnectedClientId()
    {
        return connectedClientId;
    }
    public void setConnectedClientId(List<String> connectedClientId)
    {
        this.connectedClientId = connectedClientId;
    }
}

Spring websocket message sender:

@Autowired
    private SimpMessagingTemplate msgTmp;
    private void propagateDvcMsg( WebDeviceStatusInfo device )
    {
        String msg = "";
        String userId =((Principal)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName()
        msgTmp.convertAndSend("/topic/connect"+userId, msg);
    }

I hope it's useful

Angelo Immediata
  • 6,635
  • 4
  • 33
  • 65