sockjs / sockjs-client

WebSocket emulation - Javascript client
MIT License
8.43k stars 1.3k forks source link

APIs returning 401 'unauthorised' while handshaking #623

Closed Shubham7ttn closed 1 year ago

Shubham7ttn commented 1 year ago

The 'info' api called by SockJs is returning 401 as we are unable to pass authorisation Bearer token to the server. Is there any way we can add this token ?

auvipy commented 1 year ago

can you please share more info? additionally can you check if you are sending right token?

Shubham7ttn commented 1 year ago

can you please share more info? additionally can you check if you are sending right token?

Hi @auvipy, basically we are trying to implement web-sockets in our project using React JS, Spring Boot through sockjs-client. To authenticate the client at the back-end, we need to send 'Bearer token' for which I'm using: const options = { headers: { 'Authorization': 'Bearer token', } }; const socket = new SockJS(socketURL, null, options);

However, this is not working as authorisation key with Bearer token value doesn't show up in the request header of the API.

Alternatively, I tried stompClient = over(socket); const headers = {'Authorization': token,}; stompClient.connect(headers, connectionOpenHandler, errorHandler, connectionCloseHandler);

Even this is also not working :( Any help will be much appreciated and I hope this much information will be enough :)

FZambia commented 1 year ago

@Shubham7ttn do I understand right that you are trying to pass custom headers with WebSocket connection from the web browser? If yes - it's not possible since there is no such API in browsers. And SockJS client options do not have headers also. You can pass auth information in Cookie (for same domain), in URL query param, or inside first message from client to server over SockJS. You can also configure reverse proxy before your app to transform url param to Authorization header.

Shubham7ttn commented 1 year ago

@Shubham7ttn do I understand right that you are trying to pass custom headers with WebSocket connection from the web browser? If yes - it's not possible since there is no such API in browsers. And SockJS client options do not have headers also. You can pass auth information in Cookie (for same domain), in URL query param, or inside first message from client to server over SockJS. You can also configure reverse proxy before your app to transform url param to Authorization header.

Hi @FZambia, Thanks for replying, I appreciate your help :)