简体   繁体   English

Stomp JS 基本认证

[英]Stomp JS Basic Auth

I've implemented a server using Spring Stomp and now I'm trying to connect to my server using stomp-js rx-stomp .我已经使用 Spring Stomp 实现了一个服务器,现在我正在尝试使用stomp-js rx-stomp连接到我的服务器。 What I find really awkward is that the JS implementation is not working, although I've managed to make it work using the Java stomp client.我发现真正尴尬的是JS实现不起作用,尽管我已经设法使用Java stomp 客户端使其工作。

Java client code( works ): Java 客户端代码(有效):

WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(createTransportClient()));
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
final String URL = "http://localhost:" + port + "/ws";
// -< Headers used for authentication
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
String user = "user1", pass = "abcd1234";
headers.add("Authorization", "Basic " + getBasicAuthToken(user, pass));
StompSession stompSession = stompClient.connect(URL, headers, new StompSessionHandlerAdapter() {
}).get(10, TimeUnit.SECONDS);

JS client code( doesn't work ): JS客户端代码(不起作用):

connect: function() {
    const stompConfig = {
        connectHeaders: {
            login: "user1",
            passcode: "abcd1234",
            Authorization: "Basic dXNlcjE6YWJjZDEyMzQ="
        },
        webSocketFactory: function() {
            return new SockJS("http://localhost:8080/ws");
        },
        reconnectDelay: 60000
    };
    rxStomp = new RxStomp.RxStomp();
    rxStomp.configure(stompConfig);
    rxStomp.activate();

    rxStomp.connected$.subscribe(res => {
        if (res === 1) console.log('connected');
        else console.log('not connected');
    });
}

First of all, I find really awkward that I see a prompt asking my to enter a username and a password.首先,当我看到一个提示要求我输入用户名和密码时,我觉得真的很尴尬。 If I enter the credentials there then the client connects successfully .如果我在那里输入凭据,则客户端连接成功 So, I thought that I must be doing something wrong regarding the connect headers .所以,我认为我在连接标头方面一定做错了什么。 As you can see, I've tried to add the Basic Auth token there, hoping that it would solve something.如您所见,我尝试在那里添加基本身份验证令牌,希望它能解决一些问题。 It doesn't.它没有。

The Java and the Javascript versions of the code, even though similar, differ in an important way.代码的 Java 和 Javascript 版本虽然相似,但在一个重要方面有所不同。 The Java version sets the Authorization header in the underlying HTTP connection of the Websocket. Java版本在ZFEA54C8FA9501D683BZB6的底层HTTP连接中设置Authorization header。 However, in the Javascript version, the HTTP connection is made, and then the Authorization header is passed as the STOMP CONNECT frame.但是,在Javascript版本中,是HTTP连接,然后Authorization header作为STOMP CONNECT帧传递。

The browser Websocket API or SockJS does not allow setting custom headers to the underlying HTTP connection, which is used by the Java version of the code in the question. The browser Websocket API or SockJS does not allow setting custom headers to the underlying HTTP connection, which is used by the Java version of the code in the question. To support authentication, the brokers need to support receiving authentication parameters as part of the CONNECT frame (exposed as connectHeaders in the JS Stomp clients).为了支持身份验证,代理需要支持接收身份验证参数作为 CONNECT 框架的一部分(在 JS Stomp 客户端中显示为connectHeaders )。

Spring does not, by default, support authentication parameters as part of the CONNECT frame.默认情况下,Spring 不支持将身份验证参数作为 CONNECT 帧的一部分。 Please see https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#websocket-stomp-authentication-token-based to support it.请参阅https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#websocket-stomp-authentication-token-based以支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM