spring-security block websocket (sockjs)

喜你入骨 提交于 2019-12-04 10:57:19

Your problem doesn't related to security. You just pass wrong arguments in Stomp connect and subscribe functions.

The connect() method also accepts two other variants if you need to pass additional headers:

client.connect(headers, connectCallback);
client.connect(headers, connectCallback, errorCallback);

where header is a map and connectCallback and errorCallback are functions.

this.stompClient.connect(this.token, (frame) => {

should be

this.stompClient.connect({}, (frame) => {

and

You can use the subscribe() method to subscribe to a destination. The method takes 2 mandatory arguments: destination, a String corresponding to the destination and callback, a function with one message argument and an optional argument headers, a JavaScript object for additional headers.

var subscription = client.subscribe("/queue/test", callback);

this.stompClient.subscribe('/topic/addMessage', this.authService.getAuthHeader(), (stompResponse) => {

should be

this.stompClient.subscribe('/topic/addMessage', (stompResponse) => {

Documentation http://jmesnil.net/stomp-websocket/doc/

@user1516873 finally I got it working:

  • passing correct parameters to STOMP fixed one problem
  • adding {transports: ["websocket"]} was not necessary (it works without it)

Problem was that I was using angular-cli server on port 4200 with proxy file like this:

{ "/rest": { "target": "http://localhost:8080", "secure": false } }

but should have been like this:

{ "/rest": { "target": "http://localhost:8080", "secure": false, "ws": true, "logLevel": "debug" } }

so through all the combinations of configuration I was always checking through 4200 proxy server and very rarely through native 8080 directly. I just didn't know that angular-cli proxy does not support when spring security is applied. I will accept your answer as you helped a lot!

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