1

I am having the following problem. I will describe 3 use cases - two which work and the other one which doesn't.

I have an AngularJS client using SockJS with STOMP. In the backend I have a Spring application. Client is in domain domainA.com, backend is in domainB.com.

var socket = new SockJS(("http://domainB.com/myApp/messages"));
stompClient = Stomp.over(socket);
stompClient.connect('guest', 'guest', function(frame) {
    ...
}

In the backend there are Cors filters and the Cross-origin calls are possible. All works fine.

Use Case 1. Client domainA, Server domainB

My application is unsecured on the backend side. I subscribe like below:

stompClient.subscribe('/topic/listen', function(message) {
    showMessage(JSON.parse(message.body).content);
});

All works fine.

Use Case 2. Client domainB, Server domainB

My application is secured on the backend side with Spring security. Authentication is done through a form - username and password. Nothing uncommon.

In this case the client is on domainB.com, same as the backend. All works fine, the difference is that I use a different subscription method:

stompClient.subscribe('/user/queue/listen', function(message) {
    showMessage(JSON.parse(message.body).content);
});

in order to benefit from getting the principal from the security session. All works well. No issues.

The JSESSIONID cookie is added to the connection request in new SockJS(("http://domainB.com/myApp/messages"));.

Use Case 3. Client domainA, Server domainB

The application is secured the same as in UC2. However, the client is now on a different domain.

The JSESSIONID is NOT added to the connection request. The connection to websocket in Spring is unauthenticated and redirected back to login page. This repeats and makes it impossible to connect.

Why is the JSESSIONID cookie not populated with the websocket requests in this case?

Cheers Adam

Adam Soliński
  • 444
  • 1
  • 8
  • 19

2 Answers2

1

As part of SockJS protocol, a http GET is sent to websocket server for negotiating the supported protocols. It's done using XmlHttpRequest which won't add any cookies stored for a different domain than its own domain the web application and scripts are served due to same-origin policy implemented in every modern web browser.

You should resort to a way of circumventing the same-origin policy.

Community
  • 1
  • 1
Ben-Hur Langoni Junior
  • 2,095
  • 1
  • 15
  • 14
0

I think you'll find the answers you are looking for here : http://spring.io/blog/2014/09/16/preview-spring-security-websocket-support-sessions

the trick to implement a HandshakeInterceptor

Francois
  • 1,851
  • 1
  • 13
  • 15