首页 > 解决方案 > Spring RSocket Security + RSocket-WebSocket-Client(浏览器)

问题描述

我正在尝试VueSpring. 我想用它rsocket来传输数据,但是一旦我添加rsocket seurityspring我得到:

'元数据格式错误'

想看看一个使用的工作示例jwt/simpleauth

标签: javascriptjavaspring-securityrsocket

解决方案


我用 Simple Auth 解决了这个问题,现在我想将此授权与 spring websecurity 同步。那些。以便 rsocket 中的路由通过 websecurity 检查授权。我知道这可以通过jwt 令牌来实现,即通过rest 向客户端发送一个jwt 令牌,但是我怎样才能在代码中做到这一点呢?JS 客户端(浏览器)和 Spring,如何生成 userdetails 令牌?以防万一,我将留下一个 simpleauth 实现的示例:

// METADATA BUILDER
    import {encodeRoute, encodeBearerAuthMetadata, encodeSimpleAuthMetadata, encodeAndAddCustomMetadata, encodeAndAddWellKnownMetadata, MESSAGE_RSOCKET_ROUTING, MESSAGE_RSOCKET_AUTHENTICATION} from "rsocket-core";
    
    export default class Metadata {
        constructor(json) {
            this.route = json['route'];
            this.auth = json['auth'];
        }
    
        toMetadata() {
            let metadata = Buffer.alloc(0);
            if (this.auth) {
                if (this.auth["type"] === 'bearer') {
                    metadata = encodeAndAddCustomMetadata(
                        metadata,
                        MESSAGE_RSOCKET_AUTHENTICATION.string,
                        encodeBearerAuthMetadata(this.auth["token"]),
                    );
                }
                if (this.auth["type"] === 'simple') {
                    metadata = encodeAndAddCustomMetadata(
                        metadata,
                        MESSAGE_RSOCKET_AUTHENTICATION.string,
                        encodeSimpleAuthMetadata(this.auth["username"], this.auth["password"]),
                    );
                }
            }
            if (this.route) {
                metadata = encodeAndAddWellKnownMetadata(
                    metadata,
                    MESSAGE_RSOCKET_ROUTING,
                    encodeRoute(this.route)
                );
            }
            return metadata;
        }
    }

// RSOCKET CLIENT CLASS
    import RSocketWebSocketClient from "rsocket-websocket-client";
    import {BufferEncoders, MESSAGE_RSOCKET_COMPOSITE_METADATA, RSocketClient,toBuffer} from "rsocket-core";
    import Metadata from "./metadata";
    
    
    export default class SpringClient {
        constructor(wsUrl, keepAlive = 60000, lifetime = 180000, dataMimeType = "application/json") {
            this.client = new RSocketClient({
                "setup": {
                    "keepAlive": keepAlive,
                    "lifetime": lifetime,
                    "dataMimeType": dataMimeType,
                    "metadataMimeType": MESSAGE_RSOCKET_COMPOSITE_METADATA.string
                },
                "transport": new RSocketWebSocketClient({
                    "url": wsUrl
                }, BufferEncoders)
            });
        }
    
        bearerAuth(token) {
            this.auth = {type: "bearer", token: token}
        }
    
        simpleAuth(username, password) {
            this.auth = {type: "simple", username: username, password: password}
        }
    
        logout() {
            this.auth = null;
        }
    
        connect(
            completeCallback = (socket) => {
            }, errorCallback = (error) => {
            }, subscribeCallback = (cancel) => {
            }
        ) {
            this.client.connect().subscribe({
                onComplete: socket => {
                    this.socket = socket;
                    completeCallback(socket);
                },
                onError: error => {
                    errorCallback(error);
                },
                onSubscribe: cancel => {
                    subscribeCallback(cancel);
                }
            });
        }
    
        requestResponse(data, route,
                        completeCallback = (data) => {
                        },
                        errorCallback = (error) => {
                        },
                        subscribeCallback = (cancel) => {
                        }
        ) {
            if (this.socket) {
                const metadata = new Metadata({
                    route: route,
                    auth: this.auth
                }).toMetadata();
                data = toBuffer(data);
                this.socket.requestResponse({
                    data,
                    metadata
                }).subscribe({
                    onComplete: data => {
                        completeCallback(data);
                    },
                    onError: error => {
                        errorCallback(error);
                    },
                    onSubscribe: cancel => {
                        subscribeCallback(cancel);
                    }
                });
            }
        }
    }
// EXAMPLE, HOW TO USE

    import SpringClient from "./springclient";
    
    this.client = new SpringClient("ws://localhost:7000/", 5000, 15000, "text/plain");
    this.client.connect(
        (socket) => {
            console.log("got connection complete");
            this.socket = socket;
        },
        (error) => {
            console.log("got connection error");
            console.error(error);
        },
        (cancel) => {
            console.log("got connection subscribe");
            /* call cancel() to abort */
        }
    )
    this.client.simpleAuth("LOGIN", "PASSWORD");
    this.client.requestResponse("MESSAGE", "ROUTE",
        (data) => {
            console.log("got response with requestResponse");
            console.log(data.data);
        },
        (error) => {
    
            console.log("got error with requestResponse");
            console.error(error);
        },
        (cancel) => {
            console.log(message);
            /* call cancel() to stop onComplete/onError */
        }
    );

推荐阅读