HTTP Authorization Header in EventSource (Server Sent Events)

前端 未结 8 2012
梦谈多话
梦谈多话 2021-02-01 01:52

I need to set an Authorization header to an HTML5 EventSource. As Server Sent Events seems to be disused since Websockets appeared, I cannot find any useful documentation. The a

8条回答
  •  佛祖请我去吃肉
    2021-02-01 02:20

    I went through quite a few posts in an attempt to figuring out if the auth token be sent in the EventSource() call. Although there are polyfill alternatives that allow adding headers : https://github.com/whatwg/html/issues/2177 while others mentioned sending auth token over ssl.

    Instead of using polyfill EventSource() or sending the auth token in the query params over ssl, send a eventSource identifier (eventSrcUUID) in the params of EventSource url over ssl as follows :-

    On user authentication, eventSrcUUID is generated along with the sseEmitter on the server and place in a sseEmitterMap.

    Client retrieves the eventSrcUUID from the response and invokes the EventSource() call with the eventSrcUUID in the param. On the server, the sseEmitterMap is referenced to retrieve the the eventSrc object. The sseEmitter object saved in session data is used to send event notifications to client.

提交回复
热议问题