1
votes

I'm following Vert.x 4's SockJS documentation and noticed that each SockJSSocket inside my handler has a .webSession() and a .webUser(). However, both these fields are empty aside from the .webSession().id()

I have an AuthHandler registered on the sub-router that this socket handler is on, but the SockJS Client in my frontend is not capable of sending credentials in the HTTP upgrade request.

How am I supposed to populate these fields for use?

1
I think you need to add the SessionHandler: router.route().handler(SessionHandler.create(store) - injecteer
As per the docs I had already added SessionHandler.create(LocalSessionStore.create(vertx)) further up in my router to handle authentication. It works fine for HTTP requests because I add the JWT bearer, but I cannot figure out how to add the JWT to the initial SockJS HTTP handshake request. Do you think I should use a different store type, and not LocalSessionStore? - foxtrotuniform6969

1 Answers

1
votes

I'm using the following straight-forward code to keep the session-like information available:

class SockJSBridge implements Handler<BridgeEvent> {

  @Override
  void handle( BridgeEvent event ) {
    
    SockJSSocket socket = event.socket()
    MultiMap headers = socket.headers()

    switch( event.type() ){
      case REGISTER:
         def user = getUserFromJWTHeader event.rawMessage.headers.authorization
         headers.add 'userId', user.id
         break

      case SEND:
         String userId = headers.get 'userId'
         socket.write new JsonObject( address:userId, body:event.rawMessage?.body ).toBuffer()
         break
     }
  }
}