HTTP headers in Websockets client API

后端 未结 11 2218
情话喂你
情话喂你 2020-11-22 10:41

Looks like it\'s easy to add custom HTTP headers to your websocket client with any HTTP header client which supports this, but I can\'t find how to do it with the JSON API.

相关标签:
11条回答
  • 2020-11-22 11:05

    You cannot add headers but, if you just need to pass values to the server at the moment of the connection, you can specify a query string part on the url:

    var ws = new WebSocket("ws://example.com/service?key1=value1&key2=value2");
    

    That URL is valid but - of course - you'll need to modify your server code to parse it.

    0 讨论(0)
  • 2020-11-22 11:06

    Sending Authorization header is not possible.

    Attaching a token query parameter is an option. However, in some circumstances, it may be undesirable to send your main login token in plain text as a query parameter because it is more opaque than using a header and will end up being logged whoknowswhere. If this raises security concerns for you, an alternative is to use a secondary JWT token just for the web socket stuff.

    Create a REST endpoint for generating this JWT, which can of course only be accessed by users authenticated with your primary login token (transmitted via header). The web socket JWT can be configured differently than your login token, e.g. with a shorter timeout, so it's safer to send around as query param of your upgrade request.

    Create a separate JwtAuthHandler for the same route you register the SockJS eventbusHandler on. Make sure your auth handler is registered first, so you can check the web socket token against your database (the JWT should be somehow linked to your user in the backend).

    0 讨论(0)
  • 2020-11-22 11:06

    Technically, you will be sending these headers through the connect function before the protocol upgrade phase. This worked for me in a nodejs project:

    var WebSocketClient = require('websocket').client;
    var ws = new WebSocketClient();
    ws.connect(url, '', headers);
    
    0 讨论(0)
  • 2020-11-22 11:07

    In my situation (Azure Time Series Insights wss://)

    Using the ReconnectingWebsocket wrapper and was able to achieve adding headers with a simple solution:

    socket.onopen = function(e) {
        socket.send(payload);
    };
    

    Where payload in this case is:

    {
      "headers": {
        "Authorization": "Bearer TOKEN",
        "x-ms-client-request-id": "CLIENT_ID"
    }, 
    "content": {
      "searchSpan": {
        "from": "UTCDATETIME",
        "to": "UTCDATETIME"
      },
    "top": {
      "sort": [
        {
          "input": {"builtInProperty": "$ts"},
          "order": "Asc"
        }], 
    "count": 1000
    }}}
    
    0 讨论(0)
  • 2020-11-22 11:08

    HTTP Authorization header problem can be addressed with the following:

    var ws = new WebSocket("ws://username:password@example.com/service");
    

    Then, a proper Basic Authorization HTTP header will be set with the provided username and password. If you need Basic Authorization, then you're all set.


    I want to use Bearer however, and I resorted to the following trick: I connect to the server as follows:

    var ws = new WebSocket("ws://my_token@example.com/service");
    

    And when my code at the server side receives Basic Authorization header with non-empty username and empty password, then it interprets the username as a token.

    0 讨论(0)
提交回复
热议问题