how to pass Authorization Bearer access token in websocket javascript client

一曲冷凌霜 提交于 2021-02-20 10:17:45

问题


My API Manager tool mandates that i should pass the Authorization Bearer access token with the websocket invocation call. They are providing samples of java code where they do that.

The bearer token is set like ("Authorization", "Bearer e2238f3a-e43c-3f54-a05a-dd2e4bd4631f") .How can i do that in javascript?

// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
final WebSocketClientHandler handler = new WebSocketClientHandler(
    WebSocketClientHandshakerFactory
        .newHandshaker(uri, WebSocketVersion.V13, null, 
            false, new DefaultHttpHeaders()
                .add("Authorization", "Bearer e2238f3a-e43c-3f54-a05a-dd2e4bd4631f"))); 

This is how they do it in java Is there a way like this to do it from my java script[Angular2] web application? How can i pass this from UI? Any Websocket bases testing tool can emulate this?


回答1:


I found this answer here: https://stackoverflow.com/a/26123316/1715121

"It is impossible for now to use the Authentication header, because of the design of Javascript WebSocket API. More information can be found in this thread: HTTP headers in Websockets client API

However, Bearer authentication type allows a request parameter named "access_token": http://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#query-param This method is compatible with websocket connection."



来源:https://stackoverflow.com/questions/47625452/how-to-pass-authorization-bearer-access-token-in-websocket-javascript-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!